day21 【二叉树】 530.二叉搜索树的最小绝对差 | 501.二叉搜索树中的众数 | 236. 二叉树的最近公共祖先

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

/**
 * 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;
    //先把结果赋为最大值
    int res = Integer.MAX_VALUE;
    public int getMinimumDifference(TreeNode root) {
        if(root == null) {
            return 0;
        }

        getMinimumDifference(root.left);

        if(pre != null) {
            res = Math.min(res, root.val - pre.val);
        }
        pre = root;

        getMinimumDifference(root.right);

        return res;   
    }
}

501.二叉搜索树中的众数

  • 501.二叉搜索树中的众数 | 题目链接

  • 代码随想录 | 讲解链接

  • 题意:给定一个有相同值的二叉搜索树(BST),找出 BST 中的所有众数(出现频率最高的元素)。

    假定 BST 有如下定义:

    结点左子树中所含结点的值小于等于当前结点的值
    结点右子树中所含结点的值大于等于当前结点的值
    左子树和右子树都是二叉搜索树
    在这里插入图片描述

  • 思路:因为是二叉搜索树,所以中序遍历时还是按照顺序的。用一个count记录当前节点值出现的次数,maxcount记录出现的重复元素的最大次数,并实时更新。

/**
 * 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 {
    int count;
    int maxCount;
    TreeNode pre;
    ArrayList<Integer> resList;

    public int[] findMode(TreeNode root) {
        count = 0;
        maxCount = 0;
        pre = null;
        resList = new ArrayList<>();

        findMode1(root);

        //遍历结果列表,存到数组中返回
        int[] res = new int[resList.size()];
        for(int i = 0; i < res.length; i++) {
            res[i] = resList.get(i);
        }
        return res;
    }

    public void findMode1(TreeNode root) {
        if(root == null) {
            return;
        }
        //中序遍历
        findMode1(root.left);

        //如果pre为null或者当前节点和pre节点不同,那当前节点的个数就是 1
        if(pre == null || pre.val != root.val) {
            count = 1;
        } else { //如果当前节点和pre节点的值相同,那count就++
            count++;
        }

        //如果count就是最大个数,就把当前节点的值加到List中
        if(count == maxCount) {
            resList.add(root.val);
        } else if(count > maxCount) { //如果当前记录的count已经比max大
            //更新maxcount的值
            maxCount = count;
            //清空此时List中的元素的值
            resList.clear();
            //把当前的众数的值加进List
            resList.add(root.val);
        }
        pre = root;

        findMode1(root.right);
    }
}

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

  • 236.二叉树的最近公共祖先 | 题目链接

  • 代码随想录 | 讲解链接

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

    百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
    在这里插入图片描述

  • 思路:用回溯法。自底向上查找,当找到满足条件的节点后,一层一层的向上返回节点。因此函数的返回值是节点。

/**
 * 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) {
        //终止条件:root为空或root本身为一个目标节点,那就返回root本身。
        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 left;
        } else if(left == null && right != null) { //与上同理
            return right;
        } else { //若左右孩子正好是两个目标节点,那就返回左右孩子的根节点
            return root;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

xuwuuu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值