代码随想录算法训练营第21天|● 530.二叉搜索树的最小绝对差 ● 501.二叉搜索树中的众数 ● 236. 二叉树的最近公共祖先

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

文字
视频
题目

1.思路

方法一:递归
最直观的想法,就是把二叉搜索树转换成有序数组,然后遍历一遍数组,就统计出来最小差值了。
其实在二叉搜素树中序遍历的过程中,我们就可以直接计算了。
需要用一个pre节点记录一下cur节点的前一个节点。
方法二:迭代
根本看不懂

2.代码实现

class Solution {
    int result=Integer.MAX_VALUE;
    TreeNode pre=null;
    public int getMinimumDifference(TreeNode root) {
        tranversal(root);
        return result;
    }
    public void tranversal(TreeNode cur){
        if(cur==null)
            return;
        tranversal(cur.left);
        if(pre!=null)
        {
            result=Math.min(result,cur.val-pre.val);
        }
        pre=cur;
        tranversal(cur.right);
    }
}

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

文字
视频
题目

1.思路

方法一:递归
分为不是二叉树的解法和根据二叉树的解法
只写了根据二叉树的解法。
这种写法优点在于 只需要遍历一遍。

2.代码实现

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;
        findMode1(root);
        int[] res=new int[resList.size()];
        for(int i=0;i<resList.size();i++)
            {
                res[i]=resList.get(i);
            }
        return res;
    }
    public void findMode1(TreeNode cur){
        if(cur==null)
            return;
        findMode1(cur.left);
        if(pre==null){
            count=1;
            //System.out.println("pre=null count=1");
        }
        else if(pre.val==cur.val){
            count++;
            //System.out.println("pre.val==cur.val");
            //System.out.println("count="+""+count);
        }
        else
            {
                count=1;
                //System.out.println("pre.val!=cur.val");
               // System.out.println("count=1");

            }
        if(count>maxcount){
            //System.out.println("count>maxcount");
            maxcount=count;
            resList.clear();
            resList.add(cur.val);
           // System.out.println(resList);
        }
        else if(count==maxcount)
            {
              //  System.out.println("count=maxcount");
                resList.add(cur.val);
              //  System.out.println(resList);
            }
        pre=cur;
        findMode1(cur.right);
    }
}

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

文字
视频
题目

1.思路

方法一:递归
1.求最小公共祖先,需要从底向上遍历,那么二叉树,只能通过后序遍历(即:回溯)实现从底向上的遍历方式。

2.在回溯的过程中,必然要遍历整棵二叉树,即使已经找到结果了,依然要把其他节点遍历完,因为要使用递归函数的返回值(也就是代码中的left和right)做逻辑判断。
在这里插入图片描述
在递归函数有返回值的情况下:如果要搜索一条边,递归函数返回值不为空的时候,立刻返回,如果搜索整个树,直接用一个变量left、right接住返回值,这个left、right后序还有逻辑处理的需要,也就是后序遍历中处理中间节点的逻辑(也是回溯)。

3.要理解如果返回值left为空,right不为空为什么要返回right,为什么可以用返回right传给上一层结果。
在这里插入图片描述
图中节点10的左子树返回null,右子树返回目标值7,那么此时节点10的处理逻辑就是把右子树的返回值(最近公共祖先7)返回上去!

2.代码实现

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root==null)
            return null;
        else if(root.val==p.val||root.val==q.val){
            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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值