Day31——Binary tree topic


25. Modes in Binary Search Trees

Topic Link: 501. Moderate Numbers in Binary Search Tree - LeetCode

Idea : Use the characteristics of the binary search tree and use in-order traversal. The nature of the binary search number is solved (two pointers compare the current node and the predecessor node, count, update, etc.)

Code implementation idea :

class Solution {
    
    
    List<Integer> list = new ArrayList<>();
    int pre = -1;
    int count = 0;
    int Maxcount = 0;
    public int[] findMode(TreeNode root) {
    
    
        dfs(root);
        int[] res = new int[list.size()];
        for(int i = 0;i<list.size();i++){
    
    
            res[i] = list.get(i);
        }
        return res;
    }
    public void dfs(TreeNode root){
    
    
        if(root==null){
    
    
            return ;
        }
        //向左遍历
        dfs(root.left);
        if(root.val==pre){
    
    
            count++;
        }else{
    
    
            count = 1;
        }
        pre = root.val;
        if(count==Maxcount){
    
    
            list.add(root.val);
        }else if(count>Maxcount){
    
    
            Maxcount = count;
            list.clear();
            list.add(root.val);
        }
        dfs(root.right);
    }
}

26. The nearest common ancestor of a binary tree

Topic link : 236. The nearest common ancestor of the binary tree - LeetCode

Idea : dfs post-order traversal, searching for common ancestors from low to top

  1. Finding the minimum common ancestor requires traversal from the bottom up, so the binary tree can only be traversed from low to high through post-order traversal (ie: backtracking).
  2. In the process of backtracking, the entire binary tree must be traversed. Even if the result has been found, other nodes must still be traversed, because the return value of the recursive function (that is, left and right in the code) must be used for logical judgment.
class Solution {
    
    
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
    
    
        return dfs(root,p,q);
    }
    public TreeNode dfs(TreeNode root,TreeNode p,TreeNode q){
    
    
        if(root==null||root==p||root==q){
    
    
            return root;
        }
        //后序遍历
        TreeNode left = dfs(root.left,p,q);
        TreeNode right = dfs(root.right,p,q);
        if(left==null&&right==null){
    
    
            return null;
        }
        else if(right!=null&&left==null){
    
    
            return right;
        }
        else if(right==null&&left!=null){
    
    
            return left;
        }
        else{
    
    
            return root;
        }
    }
}

27. Binary search tree nearest common ancestor

Topic link: 235. The nearest common ancestor of the binary search tree - LeetCode

Idea: Use the nature of the binary search tree: the values ​​of the left subtree are smaller than the root, and the values ​​of the right subtree are larger than the root, so that we can determine the direction of the search!

  • If rootthe value of the current root node is q pgreater , then p qthe nearest common node is in the left subtree, and we can traverse the left subtree
  • If rootthe value of the current root node is q psmaller , then p qthe nearest common node is in the right subtree, and we can traverse the left subtree
  • p qEach is in the current rootleft and right subtrees, then rootit is the nearest common ancestor at this time

Code:

Recursive method :

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

Iterative method :

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

Guess you like

Origin blog.csdn.net/weixin_54040016/article/details/127842493