力扣爆刷第143天之二叉树五连刷(二叉搜索树)

力扣爆刷第143天之二叉树五连刷(二叉搜索树)

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

题目链接:https://leetcode.cn/problems/minimum-absolute-difference-in-bst/description/
思路:求二叉搜索树任意两个节点之间的最小差值,可以利用二叉搜索树的特性,使用中序遍历,二叉搜素树是单调递增的,按照这个顺序遍历即可得到最小绝对值差。

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

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

题目链接:https://leetcode.cn/problems/find-mode-in-binary-search-tree/description/
思路:本题记录众数需要中序遍历二叉搜索树,利用递增的特性,方便统计,统计时,前后元素不等置1,相等累加,如果数值大于最大值,则情况记录数组,如果等于则添加。这样是符合众数的定义的。

/**
 * 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 {
    List<Integer> list = new ArrayList<>();
    int count = 0, max = 0;
    TreeNode pro = null;
    public int[] findMode(TreeNode root) {
        traverse(root);
        int[] nums = new int[list.size()];
        for(int i = 0; i < nums.length; i++) {
            nums[i] = list.get(i);
        }
        return nums;
    }

    void traverse(TreeNode root) {
        if(root == null) return;
        traverse(root.left);
        
        if(pro == null || pro.val != root.val) {
            count = 1;
        }else{
            count++;
        }
        pro = root;
        if(count > max) {
            list.clear();
            max = count;
            list.add(root.val);
        }else if(count == max){
            list.add(root.val);
        }

        traverse(root.right);
    }
}

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

题目链接:https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-tree/description/
思路:求最近公共祖先,非常简单,因为要求祖先,所以使用后续遍历,后续遍历,可以拿到左右子树的遍历结果,只需要比较当前节点是否是q或者p,是的话就返回。然后根据左右子树的返回结果,都为null说明没找到,都不为null说明,当前节点就是祖先,如果只有一个不为null,则该值可能是一个节点的父类,也可能是两个的父类。

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

四、235. 二叉搜索树的最近公共祖先

题目链接:https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-search-tree/description/
思路:求二叉搜索树的最近公共祖先,需要利用二叉搜索树的特性,从上往下进行搜索,通过全部大于或者全部小于决定递归方向,一旦当前节点的大小介于q和p之间,则该节点一定是最近公共祖先。其余情况按照路径返回。

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == p || root == q) return root;
        if(root.val > p.val && root.val > q.val) {
            return lowestCommonAncestor(root.left, p, q);
        } 
        if(root.val < p.val && root.val < q.val) {
            return lowestCommonAncestor(root.right, p, q);
        }
        return root;
    }

}

五、701. 二叉搜索树中的插入操作

题目链接:https://leetcode.cn/problems/insert-into-a-binary-search-tree/description/
思路:构造树,需要利用二叉搜素树的特性,而且还需要在搜锁的过程中构建树。

class Solution {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        if(root == null) {
            return new TreeNode(val);
        }else if(root.val > val) {
            root.left = insertIntoBST(root.left, val);
        }else{
            root.right = insertIntoBST(root.right, val);
        }
        return root;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

当年拼却醉颜红

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

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

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

打赏作者

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

抵扣说明:

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

余额充值