美文网首页
算法|二叉搜索树的最小绝对差、二叉搜索树中的众数、二叉树的最近公

算法|二叉搜索树的最小绝对差、二叉搜索树中的众数、二叉树的最近公

作者: 激扬飞雪 | 来源:发表于2022-12-05 15:53 被阅读0次

    530. 二叉搜索树的最小绝对差

    题目连接:https://leetcode.cn/problems/minimum-absolute-difference-in-bst/
    思路一:使用中序遍历,前后两个之差是最小的绝对差 (递归法)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode() {}
     *     TreeNode(int val) { this.val = val; }
     *     TreeNode(int val, TreeNode left, TreeNode right) {
     *         this.val = val;
     *         this.left = left;
     *         this.right = right;
     *     }
     * }
     */
    class Solution {
        private TreeNode pre;
        private int minV = Integer.MAX_VALUE;
        private void getMin(TreeNode root) {
            if (root == null) return;
            getMin(root.left);
            if (pre != null) minV = Math.min(minV, root.val - pre.val);
            pre = root;
            getMin(root.right);
        }
        public int getMinimumDifference(TreeNode root) {
          getMin(root);
          return minV;
        }
    }
    

    思路二:使用迭代法中序遍历

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode() {}
     *     TreeNode(int val) { this.val = val; }
     *     TreeNode(int val, TreeNode left, TreeNode right) {
     *         this.val = val;
     *         this.left = left;
     *         this.right = right;
     *     }
     * }
     */
    class Solution {
        public int getMinimumDifference(TreeNode root) {
            Stack<TreeNode> stack = new Stack<>();
            TreeNode cur = root;
            TreeNode pre = null;
            int minV = Integer.MAX_VALUE;
            while (cur != null || !stack.isEmpty()){
                if (cur != null) {
                    stack.push(cur);
                    cur = cur.left;
                } else {
                    TreeNode treeNode = stack.pop();
                    if (pre != null) minV = Math.min(minV, treeNode.val - pre.val);
                    pre = treeNode;
                    cur = treeNode.right;
                }
            }
            return minV;
        }
    }
    

    二、 501. 二叉搜索树中的众数

    题目连接:https://leetcode.cn/problems/find-mode-in-binary-search-tree/
    思路一:使用二叉搜索树中序遍历单调递增有序的特性,使用中序遍历,统计出现的频次,前后相等的count++,前后不相等的或者第一个元素count = 1; if (count == maxCount) 将这个元素加入结合中,如果有比maxcount更大的值,则清楚集合,加入新集合
    1、递归法

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode() {}
     *     TreeNode(int val) { this.val = val; }
     *     TreeNode(int val, TreeNode left, TreeNode right) {
     *         this.val = val;
     *         this.left = left;
     *         this.right = right;
     *     }
     * }
     */
    class Solution {
        private TreeNode pre;
        private int count = 0;
        private int maxCount = 0;
        private void findMode(TreeNode root, List<Integer> list) {
            if (root == null) return ;
            findMode(root.left, list);
            if (pre == null) {
                count = 1;
            } else if (pre.val == root.val) {
                count++;
            } else {
                count = 1;
            }
            if (count == maxCount) {
                list.add(root.val);
            } else if (count > maxCount){
                list.clear();
                list.add(root.val);
                maxCount = count;
            }
            pre = root;
            findMode(root.right, list);
        }
        public int[] findMode(TreeNode root) {
            List<Integer> list = new ArrayList<>();
            findMode(root, list);
            int[] result = new int[list.size()];
            for (int i = 0; i < result.length; i++){
                result[i] = list.get(i);
            }
            return result;
        }
    }
    

    2、使用迭代法中序

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode() {}
     *     TreeNode(int val) { this.val = val; }
     *     TreeNode(int val, TreeNode left, TreeNode right) {
     *         this.val = val;
     *         this.left = left;
     *         this.right = right;
     *     }
     * }
     */
    class Solution {
        public int[] findMode(TreeNode root) {
            List<Integer> list = new ArrayList<>();
            Stack<TreeNode> stack = new Stack<>();
            TreeNode pre = null;
            int count = 0;
            int maxCount = 0;
            TreeNode cur = root;
            while (cur != null || !stack.isEmpty()){
                if (cur != null) {
                    stack.push(cur);
                    cur = cur.left;
                } else {
                    TreeNode treeNode = stack.pop();
                    if (pre == null || pre.val != treeNode.val) {
                        count = 1;
                    } else {
                        count++;
                    }
                    if (count == maxCount) {
                        list.add(treeNode.val);
                    } else if (count > maxCount) {
                        list.clear();
                        list.add(treeNode.val);
                        maxCount = count;
                    }
                    pre = treeNode;
                    cur = treeNode.right;
                }
            }
    
            int[] result = new int[list.size()];
            for (int i = 0; i < result.length; i++){
                result[i] = list.get(i);
            }
            return result;
        }
    }
    

    236. 二叉树的最近公共祖先

    题目连接:https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-tree/
    思路:最新公共祖先,使用后序遍历,左边不为空,右边不为空返回中间节点,即使最近公共祖先

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
            if (root == null) return null;
            if (root == p || root == q) return root;
            TreeNode leftNode = lowestCommonAncestor(root.left, p, q);
            TreeNode rightNode = lowestCommonAncestor(root.right, p, q);
            if (leftNode != null && rightNode != null) return root;
            else if (leftNode != null && rightNode == null) return leftNode;
            else if (leftNode == null && rightNode != null) return rightNode;
            else return null;
        }
    }
    

    相关文章

      网友评论

          本文标题:算法|二叉搜索树的最小绝对差、二叉搜索树中的众数、二叉树的最近公

          本文链接:https://www.haomeiwen.com/subject/dpfifdtx.html