【算法刷题day21】Leetcode:530. 二叉搜索树的最小绝对差、501. 二叉搜索树中的众数、236. 二叉树的最近公共祖先

草稿图网站
java的Deque

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

题目:530. 二叉搜索树的最小绝对差
解析:代码随想录解析

解题思路

中序遍历,每次记录上一个节点pre。然后比较和记录minDiff

代码

/**
 * 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 {
    TreeNode pre = null;
    int minDiff = Integer.MAX_VALUE;
    public int getMinimumDifference(TreeNode root) {
        inorder(root);
        return minDiff;
    }
    private void inorder(TreeNode node) {
        if (node == null)
            return;
        inorder(node.left);
        if (pre != null)
            minDiff = Math.min(minDiff, node.val - pre.val);
        pre = node;
        inorder(node.right);
    }
}

//借助栈的中序遍历
class Solution {
    public int getMinimumDifference(TreeNode root) {
        int minDiff = Integer.MAX_VALUE;
        TreeNode pre = null;
        TreeNode cur = root;
        Stack<TreeNode> stack = new Stack<>();
        while (cur != null || !stack.isEmpty()) {
            if (cur != null) {
                stack.push(cur);
                cur = cur.left;
            } else {
                cur = stack.pop();
                if (pre != null)
                    minDiff = Math.min(minDiff, cur.val - pre.val);
                pre = cur;
                cur = cur.right;
            }
        }
        return minDiff;
    }
}

总结

中序遍历

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

题目:501. 二叉搜索树中的众数
解析:代码随想录解析

解题思路

中序遍历

代码

/**
 * 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 {
    List<Integer> res = new ArrayList<>();
    int maxModeCount = 0;
    int mode = Integer.MIN_VALUE;
    int modeCount = 0;
    public int[] findMode(TreeNode root) {
    	//如果root为空,则返回空数组
        if (root == null)
            return new int[0];
        //否则,至少有1个众数
        traversal(root);
        int[] array = new int[res.size()];
        for (int i = 0; i < res.size(); i++) {
            array[i] = res.get(i);
        }
        return array;
    }
    private void traversal(TreeNode node) {
        if (node == null)
            return;
        traversal(node.left);
        //如果和当前值相同则众数的count++,否则重新记录mode,count设为1
        if (node.val == mode)
            modeCount++;
        else {
            mode = node.val;
            modeCount = 1;
        }
        //因为至少有个一个数,所以肯定不会返回Integer.MIN_VALUE
        //若超过众数的最大值,则清空重新加入,并记录众数的最大个数
        if (modeCount > maxModeCount) {
            maxModeCount = modeCount;
            res.clear();
            res.add(mode);
        } else if (modeCount == maxModeCount) {
            res.add(mode);
        }
        traversal(node.right);
    }
}

总结

见代码注释

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

题目:236. 二叉树的最近公共祖先
解析:代码随想录解析

解题思路

后序遍历,递归。若到null,p,q的时候返回。
然后left和right分别进行搜索,如果都为null则返回null。
如果有一边不为null,则找到p或q中的一个。
如果两边都不为null,则root为一个最小公共祖先。(如果root是最小公共祖先的祖先的话,上面的节点只有返回left或right,所以最终返回的肯定是最小公共祖先)

代码

/**
 * 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 || 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)//right找到一个节点
            return right;
        else if (left != null && right == null)//left找到一个节点
            return left;
        else//left和right都找到一个节点
            return root;
    }
}

总结

妙啊

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Allmight_Q

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值