【代码随想录|刷题记录Day21】530.二叉搜索树的最小绝对差、501.二叉搜索树中的众数、236.二叉树的最近公共祖先

30 阅读2分钟

题目列表

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

  501.二叉搜索树中的众数

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

解题过程

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

给你一个二叉搜索树的根节点 root ,返回 树中任意两不同节点值之间的最小差值

差值是一个正数,其数值等于两值之差的绝对值。

思路: 利用二叉搜索树的有序特性。

递归法

class Solution {
    TreeNode pre; //记录上一个遍历的节点
    int result = Integer.MAX_VALUE; //初始化最小绝对差
    public int getMinimumDifference(TreeNode root) {
        if (root == null) {
            return 0;
        }
        traveral(root);
        return result;
    }

    public void traveral(TreeNode root) {
        if (root == null) {
            return;
        }
        traveral(root.left);
        if (pre != null) {
            result = Math.min(result, Math.abs(root.val - pre.val));
        }
        pre = root;
        traveral(root.right);
    }
}

迭代法--中序遍历

class Solution {
    public int getMinimumDifference(TreeNode root) {
        if (root == null) {
            return 0;
        }
        TreeNode pre = null; //记录上一个遍历的节点
        Stack<TreeNode> stack = new Stack<>();
        int result = Integer.MAX_VALUE;
        TreeNode cur = root;
        while (cur != null || !stack.isEmpty()) {
            if (cur != null) {
                stack.push(cur);
                cur = cur.left;
            } else {
                cur = stack.pop();
                if (pre != null) {
                    result = Math.min(result, Math.abs(cur.val - pre.val));
                }
                pre = cur;
                cur = cur.right;
            }
        }
        return result;
    }
}

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

给你一个含重复值的二叉搜索树(BST)的根节点 root ,找出并返回 BST 中的所有众数(即,出现频率最高的元素)。

如果树中有不止一个众数,可以按 任意顺序 返回。

思路: 二叉搜索树,中序遍历就是有序的。在遍历过程中统计val出现的个数并加入结果集(有被清空的可能)。

递归法

class Solution {
    ArrayList<Integer> resList;
    int maxCount;
    int count;
    TreeNode pre; //上一次遍历的节点
    public int[] findMode(TreeNode root) {
        resList = new ArrayList<>();
        maxCount = 0;
        count = 0;
        pre = null;
        traveral(root);
        int[] res = new int[resList.size()];
        for (int i = 0; i < resList.size(); i++) {
            res[i] = resList.get(i);
        }
        return res;
    }

    public void traveral(TreeNode root) {
        if (root == null) {
            return;
        }
        traveral(root.left);
        // 计数
        if (pre == null || root.val != pre.val) {
            count = 1;
        } else {
            count++;
        }
        // 更新结果集合及maxCount
        if (count > maxCount) {
            resList.clear();
            resList.add(root.val);
            maxCount = count;
        } else if (count == maxCount) {
            resList.add(root.val);
        }
        pre = root;
        traveral(root.right);
    }
}

迭代法

class Solution {
    public int[] findMode(TreeNode root) {
        int maxCount = 0;
        int count = 0;
        TreeNode pre = null; //上一次遍历的节点
        ArrayList<Integer> resList = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while (cur != null || !stack.isEmpty()) {
            if (cur != null) {
                stack.push(cur);
                cur = cur.left;
            } else {
                cur = stack.pop();
                // 计数
                if (pre == null || cur.val != pre.val) {
                    count = 1;
                } else {
                    count++;
                }
                // 更新结果集合及maxCount
                if (count > maxCount) {
                    resList.clear();
                    resList.add(cur.val);
                    maxCount = count;
                } else if (count == maxCount) {
                    resList.add(cur.val);
                }
                pre = cur;
                cur = cur.right;
            }
        }
        int[] res = new int[resList.size()];
        for (int i = 0; i < resList.size(); i++) {
            res[i] = resList.get(i);
        }
        return res;
    }
}

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

给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。

思路: 自底向上查找(回溯)。后序遍历(左右中)就是天然的回溯过程,可以根据左右子树的返回值,来处理中节点的逻辑。

如果找到一个节点,发现左子树出现节点p,右子树出现节点q,或者 左子树出现结点q,右子树出现节点p,那么该节点就是节点p和q的最近公共祖先。

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null || root == p || root == q) {
            return root;
        }

        TreeNode left = lowestCommonAncestor(root.left, p, q);
        TreeNode right = lowestCommonAncestor(root.right, p, q);

        if (left == null && right == null) {
            return null;
        } else if (left == null && right != null) {
            return right;
        } else if (left != null && right == null) {
            return left;
        } else {
            return root;
        }
    }
}