刷题日记:530. 二叉搜索树的最小绝对差|501. 二叉搜索树中的众数

文章介绍了如何利用中序遍历解决二叉搜索树中的两个问题:找到树中所有节点值的最小绝对差和查找众数。对于最小绝对差问题,通过中序遍历得到递增数组,用双指针计算差值;对于众数问题,同样用中序遍历,通过计数和比较更新最大出现次数及结果集合。
摘要由CSDN通过智能技术生成

题目链接: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;
        }
        getRes(root);
        return res;
    }

//中序遍历二叉搜索树 得到的是有序数组  
    private void getRes(TreeNode cur){
        if(cur == null){
            return;
        }
        getRes(cur.left);
        if(pre != null){
            res = res < (cur.val - pre.val)?res:(cur.val - pre.val);
        }
        //移动pre
        pre = cur;
        getRes(cur.right);
    }
}

总结:用pre指向前一个元素(开始的时候为null),用cur指向当前遍历的节点,同时pre指的位置也相当于是左孩子,我们采用中续遍历来遍历这个二叉树,顺序为左中右。不断的更新res,遍历完之后就可以得到最小的差值了。


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 {
    int count =0;
    int maxCount = 0;
    TreeNode pre;
    List<Integer> res = new ArrayList<>();
    public int[] findMode(TreeNode root) {
        treaval(root);
        int[] resNum = new int[res.size()];
        for(int i=0;i<res.size();i++){
            resNum[i] = res.get(i);
        }
        return resNum;
    }

    public void treaval(TreeNode cur){
        //递归终止条件
        if(cur == null){
            return;
        }
        treaval(cur.left);

        if(pre == null || cur.val != pre.val){
            count = 1;
        }else if(cur.val == pre.val){
            count++;
        }
        //不断更新结果集的数据
        if(count == maxCount){
            res.add(cur.val);
        }
        if(count > maxCount){
            res.clear();
            maxCount = count;
            res.add(cur.val);
        }
        //移动pre   
        pre = cur;

        treaval(cur.right);
    }
}

总结:思路和上一题大差不差,核心思想就是在更新结果集数据的时候逻辑不一样,如果cur这个节点的count和maxcount相等,那么说明此时这个cur的值是符合题目要求的(注意,是此时不是最终结果),继续往后遍历,如若出现count比maxcount大,那么说明之前收集的结果都是无效的,此时需要清空list,更新maxcount的数值,并且将cur的值加入到list中,继续后面的操作:更新pre以及向右子树遍历,最终得到结果。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值