代码随想录Day_21打卡

①、二叉搜索树的最小绝对值差

给你一个二叉搜索树的根节点 root ,返回 树中任意两不同节点值之间的最小差值 。

差值是一个正数,其数值等于两值之差的绝对值。

事例:

输入:root = [4,2,6,1,3]
输出:1

思路:

        利用二叉搜索树中序遍历为升序的特性,在中序遍历二叉搜索树的时候,判断相邻差值,记录最小值。

代码:

// private TreeNode pre = null;
    // private int res = Integer.MAX_VALUE;
    public int getMinimumDifference(TreeNode root) {
        //inorder(root);
        
        //迭代
        int res = Integer.MAX_VALUE;
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        int count = 0;
        List<Integer> help = new ArrayList<>();
        while(!stack.isEmpty() || cur != null){
            if(cur != null){
                while(cur != null){
                    stack.push(cur);
                    cur = cur.left;
                }
            }else{
                cur = stack.pop();
                help.add(cur.val);
                cur = cur.right;
                count++;
                if(count > 1){
                    int tmp = help.get(count - 1) - help.get(count - 2);
                    res = Math.min(tmp,res);
                }
            }
        }
        return res;
    }

    // public void inorder(TreeNode root){
    //     if(root == null) return;
    //     inorder(root.left);
    //     if(pre != null){
    //         res = Math.min(res,root.val - pre.val);
    //     }
    //     pre = root;
    //     inorder(root.right);
    // }

注释部分代码为递归做法

②、二叉搜索树中的众数

给你一个含重复值的二叉搜索树(BST)的根节点 root ,找出并返回 BST 中的所有 众数(即,出现频率最高的元素)。

如果树中有不止一个众数,可以按 任意顺序 返回。

假定 BST 满足如下定义:

  • 结点左子树中所含节点的值 小于等于 当前节点的值
  • 结点右子树中所含节点的值 大于等于 当前节点的值
  • 左子树和右子树都是二叉搜索树

事例:

输入:root = [1,null,2,2]
输出:[2]

思路:

        普通二叉树需要遍历一边,将频数和val值存入map中,然后将map转为list 排序,最后构造成数组返回。

        这道题可以利用二叉搜索树的特性,即中序遍历途中直接记录频数,并随时更新最大频数,若出现不同数,则更改当前频数为1,记录不同值的频数,最后用list保存最大频数的数。当前频数,最大频数,list都可以直接使用全局变量,方便递归遍历途中访问。

代码:

 private int maxCount = Integer.MIN_VALUE;
    private int curCount = 0;
    private List<Integer> res = new ArrayList<>();
    private TreeNode pre = null;
    public int[] findMode(TreeNode root) {
        inorder(root);
        int n = res.size();
        int[] nums = new int[n];
        int count = 0;
        for(int x : res){
            nums[count++] = x;
        }
        return nums;
    }

    public void inorder(TreeNode root){
        if(root == null) return;
        inorder(root.left);
        if(pre == null) curCount = 1;
        else if(pre.val == root.val) curCount++;
        else curCount = 1;
        pre = root;
        if(curCount > maxCount){
            // 更新频率,清除之前最大频数保存的数值。
            res.clear();
            maxCount = curCount;
            res.add(root.val);
        }else if(curCount == maxCount){
            // 跟最大频率相同 直接保存
            res.add(root.val);
        }

        inorder(root.right);
    }

③、二叉树的最近公共祖先

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

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

事例:

输入:root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
输出:3
解释:节点 5 和节点 1 的最近公共祖先是节点 3

思路:

        这道题需要寻找两个子节点的最近公共祖先,即需要自底向上的处理方式,故采用后序遍历处理。(先在左右子树寻找是否有最近公共祖先,再判断当前结点是否是结果)。

        采用后序遍历,若当前root为空 或者为p 或者为q时,返回root,若不返回,则分别递归左右子树,保存左右子树的遍历结果。若左右子树的结果都不为空,则当前root是结果,若不是,返回不为null的左右孩子,若左右孩子均为空,则表示p或q不出现在当前结点和其子树上。

代码:

public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        return partition(root,p,q);
    }

    public TreeNode partition(TreeNode root,TreeNode p,TreeNode q){
        if(root == null || root == p || root == q) return root;

        //遍历左子树
        TreeNode left = partition(root.left,p,q);

        //遍历右子树
        TreeNode right = partition(root.right,p,q);

        if(left != null && right != null){
            //最近公共祖先
            return root;
        }else if(left == null){
            return right;
        }else if(right == null){
            return left;
        }else{
            return null;
        }
    }

参考:代码随想录 (programmercarl.com)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值