day18 二叉树-minAbosoluteDiff+modeInBST+lowestCommonAncestor

### 6.21 530. Minimum Absolute Difference in BST
Given the root of a Binary Search Tree (BST), return the minimum absolute difference between the values of any two different nodes in the tree.
https://programmercarl.com/0530.%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E7%9A%84%E6%9C%80%E5%B0%8F%E7%BB%9D%E5%AF%B9%E5%B7%AE.html 
```java
public class minDifferenceInBST {  
    //双指针  
    int result = Integer.MAX_VALUE;  
    TreeNode pre = null;  
    public int getMinimumDifference(TreeNode root) {  
        traversal(root);  
        return result;  
    }  
    public void traversal(TreeNode cur){  
        if(cur == null) return;  
        //左  
        traversal(cur.left);  
        //中  
        if(pre != null){  
            result = Math.min(result,cur.val - pre.val);//取最小值  
        }  
        pre = cur;  
        //右  
        traversal(cur.right);  
    }    
}
```

### 6.22 501. Find Mode in Binary Search Tree
Given the root of a binary search tree (BST) with duplicates, return all the mode(s) (i.e., the most frequently occurred element) in it.
If the tree has more than one mode, return them in any order.
Assume a BST is defined as follows:
The left subtree of a node contains only nodes with keys less than or equal to the node's key.
The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
Both the left and right subtrees must also be binary search trees.
https://programmercarl.com/0501.%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E4%B8%AD%E7%9A%84%E4%BC%97%E6%95%B0.html  
```java
public class modeInBST {  
    TreeNode pre = null;  
    int count = 0;  
    int maxCount = 0;  
    List<Integer> result = new LinkedList<>();  
    public int[] findMode(TreeNode root) {  
        traversal(root);  
        int size = result.size();  
        int[] res = new int[size];  
        for (int i = 0; i < size; i++) {  
            res[i] = result.get(i);  
        }  
        return res;  
    }  
    private void traversal(TreeNode root){  
        if(root == null) return;  
        //左  
        traversal(root.left);  
        //中  
        if(pre == null || pre.val != root.val){  
            count = 1;  
        }else{  
            count++;  
        }  
        pre = root;  
        //当这个数字出现频率比最大频率高时,清空list,增加新元素。  
        if(count == maxCount){  
            result.add(root.val);  
        }else if(count > maxCount){  
            maxCount = count;  
            result.clear();  
            result.add(root.val);  
        }  
        //右  
        traversal(root.right);  
    }  
  
}
```

 
### 6.23 236. Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”
https://programmercarl.com/0236.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E6%9C%80%E8%BF%91%E5%85%AC%E5%85%B1%E7%A5%96%E5%85%88.html 

```java
public class lowestCommonAncestor {  
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {  
        if(root == null || root == p || root == q) 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;  
        }  
    }  
}
```

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值