代码随想录打卡第21天|530.二叉搜索树的最小绝对差;501.二叉搜索树中的众数;236. 二叉树的最近公共祖先

本文介绍了如何在二叉搜索树中找到最小绝对差、众数以及最近公共祖先。使用双指针和中序遍历是解决问题的关键,对于最小差值问题,通过比较当前节点与前一个节点的值来更新最小差值;在找众数时,利用中序遍历统计相同值的节点数量;而最近公共祖先则通过后序遍历来确定。
摘要由CSDN通过智能技术生成

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

关键点1:采用双指针法,pre,cur,预设pre,minValue

关键点2:采用中序遍历,每次中遍历时,判断cur与pre节点之间的数值差与minValue的关系

class Solution {
    int minValue = Integer.MAX_VALUE;
    TreeNode pre = null;
    public int getMinimumDifference(TreeNode root) {
        if(root == null){
            return 0; 
        }
        traversal(root);
        return minValue;
    }
    public void traversal(TreeNode cur){
        if(cur == null){
            return; 
        }
        traversal(cur.left);  // 左
        if(pre != null ){     // 中
            minValue =  Math.min(cur.val - pre.val,minValue);
        }
        pre = cur;
        traversal(cur.right);  //右   
    }
}

501.二叉搜索树中的众数

关键点1:采用双指针法,pre,cur,预设pre,maxCount,count

关键点2:采用中序遍历

2-1:每次中遍历时,判断cur与pre是否相等,相等count++;不相等就把count置为1

2-2:判断count与maxCount之间的关系,如果count > maxCount,更新maxCount,清楚结果集里的节点数值,重新添加最新的众数节点数值进去;如果count == maxCount,添加此时的节点数值进去

class Solution {
    List<Integer> list = new ArrayList<>();
    TreeNode pre = null;
    int maxCount = 0;
    int count = 0;
    public int[] findMode(TreeNode root) {
        
        traversal(root);
        int[] res = new int[list.size()] ;
        int j = 0;
        for(int i: list){
            res[j++] = i;
        }
        return res;

    }
    public void traversal(TreeNode cur){
        if(cur == null){
            return;
        }
        traversal(cur.left);

        if(pre == null || pre.val != cur.val){
            count = 1;
        }else{
            count++;
        }
        if(count > maxCount) {
            maxCount = count;
            list.clear();
            list.add(cur.val);
        }else if(count == maxCount) {
            list.add(cur.val);
        }    
        pre = cur;
        traversal(cur.right);

    }
}

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

关键点1:采用后序遍历

关键点2:

2-1:root == null,返回root:

2-2:root == p || root == q,返回root

关键点3:确定几个条件

3-1:左结果为空,右结果为空,往上返回null

3-2:左结果不为空,右结果为空,往上返回左结果

3-3:左结果为空,右结果不为空,往上返回右结果

3-4:左结果不为空,右结果不为空,返回root

 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值