代码随想录算法训练营第二十一天| LeetCode530 二叉搜索树的最小绝对差 、LeetCode501 二叉搜索树中的众数 、LeetCode236 二叉树的最近公共祖先

LeetCode530 二叉搜索树的最小绝对差

题目链接:https://programmercarl.com/0530.%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E7%9A%84%E6%9C%80%E5%B0%8F%E7%BB%9D%E5%AF%B9%E5%B7%AE.html代码:

public class code530 {
    TreeNode preNode = null;
    int minDiff = Integer.MAX_VALUE;
    public int getMinimumDifference(TreeNode root) {
        if (root == null) return 0;
        transfer(root);
        return minDiff;
    }

    private void transfer(TreeNode root) {
        if (root == null) {
            return;
        }
        // 左
        transfer(root.left);
        // 中
        if (preNode != null) {
            minDiff = Math.min(minDiff, root.val - preNode.val);
        }
        preNode = root;
        // 右
        transfer(root.right);
    }
}

依然是中序遍历加双指针的思想

LeetCode501 二叉搜索树中的众数

题目链接:https://programmercarl.com/0501.%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E4%B8%AD%E7%9A%84%E4%BC%97%E6%95%B0.html代码:

public class code501 {
    TreeNode preNode;
    int count;
    int maxCount;
    List<Integer> res;
    public int[] findMode(TreeNode root) {
        preNode = null;
        count = 0;
        maxCount = 0;
        res = new ArrayList<>();
        transfer(root);
        int[] resList = new int[res.size()];
        for (int i = 0; i < res.size(); i++) {
            resList[i] = res.get(i);
        }
        return resList;
    }

    private void transfer(TreeNode root) {
        if (root == null) {
            return;
        }
        // 左
        transfer(root.left);
        // 计算当前遍历节点的数量
        if (preNode == null) {
            count = 1;
        } else if (preNode.val == root.val) {
            count++;
        } else {
            count = 1;
        }
        // 更新maxCount
        if (count == maxCount) {
            res.add(root.val);
        } else if (count > maxCount) {
            maxCount = count;
            res.clear();
            res.add(root.val);
        }
        preNode = root;
        // 右
        transfer(root.right);
    }
}

体会更新maxCount时对数组的操作

LeetCode236 二叉树的最近公共祖先

题目链接:https://programmercarl.com/0236.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E6%9C%80%E8%BF%91%E5%85%AC%E5%85%B1%E7%A5%96%E5%85%88.html代码:

public class code236 {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null) return null;
        if (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 root;
        } else if (left == null && right != null) {
            return right;
        } else if (left != null && right == null) {
            return left;
        } else {
            return null;
        }
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值