目录
🎈LeetCode530.二叉搜索树的最小绝对差
🎈LeetCode501.二叉搜索树中的众数
🎈LeetCode236.二叉树的最近公共祖先
🎈LeetCode530.二叉搜索树的最小绝对差
链接:530.二叉树的最小绝对差
给你一个二叉搜索树的根节点
root,返回 树中任意两不同节点值之间的最小差值 。差值是一个正数,其数值等于两值之差的绝对值。

int result=Integer.MAX_VALUE;
    TreeNode pre=null;
    public int getMinimumDifference(TreeNode root) {
        traversal(root);
        return result;
    }
    public void traversal(TreeNode root){
        if(root==null){
            return;
        }
        traversal(root.left);
        if(pre!=null){
            result=Math.min(result,root.val-pre.val);
        }
        pre=root;
        traversal(root.right);
    }🎈LeetCode501.二叉搜索树中的众数
链接:501.二叉搜索树的中的众数
给你一个含重复值的二叉搜索树(BST)的根节点
root,找出并返回 BST 中的所有 众数(即,出现频率最高的元素)。如果树中有不止一个众数,可以按 任意顺序 返回。
假定 BST 满足如下定义:
- 结点左子树中所含节点的值 小于等于 当前节点的值
- 结点右子树中所含节点的值 大于等于 当前节点的值
- 左子树和右子树都是二叉搜索树

List<Integer> list=new ArrayList<>();
    int count=0;
    int max=0;
    TreeNode pre=null;
    public int[] findMode(TreeNode root) {
        // 二叉搜索树的解法,中序遍历
        searchBST(root);
        int[] result=new int[list.size()];
        for(int i=0;i<list.size();i++){
            result[i]=list.get(i);
        }
        return result;
    }
    public void searchBST(TreeNode root){
        if(root==null){
            return;
        }
        searchBST(root.left); 
        if(pre==null){
            count=1;
        }else if(pre.val==root.val){
            count++;
        }else{
            count=1;
        }
        if(count>max){
            max=count;
            list.clear();
            list.add(root.val);
        }else if(count==max){
            list.add(root.val);
        }
        pre=root;
        searchBST(root.right);
        return;
    }🎈LeetCode236.二叉树的最近公共祖先
链接:236.二叉树的最近公共祖先
给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。
百度百科中最近公共祖先的定义为:“对于有根树 T 的两个节点 p、q,最近公共祖先表示为一个节点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
 
 
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root==p || root==q || root==null){
            return root;
        }
        TreeNode left=lowestCommonAncestor(root.left,p,q);
        TreeNode right=lowestCommonAncestor(root.right,p,q);
        if(left==null && right!=null){
            return right;
        }else if(left!=null && right==null){
            return left;
        }else if(left!=null && right!=null){
            return root;
        }else{
            return null;
        }
    }


















