[Likou brush question] Day15 - Binary tree topic


Previous article: [Likou Brushing Questions] Day14——Binary Tree Topic_Tatakai!!!'s Blog-CSDN Blog

3. Level order traversal of binary tree

Topic link: 102. Level order traversal of binary tree - LeetCode

BFSBare question: the head (root) enters the queue first, when the queue is not empty, (traversing all the elements in the queue at this time) the head elements are dequeued one by one (the head of the queue), and the elements of the answer (each layer) are added, Then the left and right sons of the element are added to the queue (entry at the end of the queue). After the current layer is traversed, the answer of this layer is recorded and entered into the next layer. until the last queue is empty

Code

class Solution {
    
    
    public List<List<Integer>> levelOrder(TreeNode root) {
    
    
        List<List<Integer>> res = new ArrayList<>();
        if(root == null){
    
    
            return res;
        }
        
        Queue<TreeNode> q = new LinkedList<>();
        q.add(root);
        while(!q.isEmpty()){
    
    
            List<Integer> ceng = new ArrayList<>();
            int len = q.size();

            for(int i = 0; i < len; i ++){
    
    
                TreeNode t =  q.poll();
                ceng.add(t.val);

                if(t.left != null) q.add(t.left);
                if(t.right != null) q.add(t.right);
            }
            res.add(ceng);
        }
        return res;
    }
}

4. Flip the binary tree

Topic Link: 226. Flip Binary Tree - LeetCode

Idea: recursive implementation

  • If the root node is not empty, then its left and right subtrees must be exchanged (even if the left and right subtrees are empty nodes)
  • Recursively swap left and right subtrees

Code

class Solution {
    
    
    public TreeNode invertTree(TreeNode root) {
    
    
        dfs(root);
        return root;
    }
    public static void dfs(TreeNode root){
    
    
        if(root == null){
    
    
            return ;
        }
        // 交换
        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;

        dfs(root.left);
        dfs(root.right);
    }
}

5. Symmetric binary tree

Topic Link: 101. Symmetric Binary Tree - LeetCode

Solution 1 Recursion: Traverse the left and right subtrees at the same time, whether the left and right subtrees are completely symmetrical

  1. The values ​​of the two root nodes must be equal
  2. The left subtree on the left is symmetrical to the right subtree on the right
  3. The right subtree on the left is symmetrical to the left subtree on the right

Code

class Solution {
    
    
    public boolean isSymmetric(TreeNode root) {
    
    
        return dfs(root.left, root.right);
    }
    public static boolean dfs(TreeNode l, TreeNode r){
    
    
        if(l == null && r == null) return true;
        if(l == null || r == null) return false;
        if(l.val != r.val) return false;
        return dfs(l.left, r.right) && dfs(l.right, r.left);
    }
}

Solution two iterations :

  • The tree on the left 左中右is traversed in order of
  • The tree on the right 右中左is traversed in order of
  • Traversing at the same time, judging whether the values ​​are the same while traversing (each traversal is a one-to-one correspondence between two points)

Stack to iterate to simulate the above recursive traversal process!

Code

class Solution {
    
    
    public boolean isSymmetric(TreeNode root) {
    
    
        if(root == null) return true;

        Stack<TreeNode> left = new Stack<>();// 左树
        Stack<TreeNode> right = new Stack<>();// 右树
        TreeNode lc = root.left;
        TreeNode rc = root.right;
        // 左边的树:左中右
        // 右边的树:右中左
        while(lc != null || rc != null || !left.empty()){
    
    
            // 同时遍历
            while(lc != null && rc != null){
    
    
                //左树: 一直左
                left.push(lc);
                lc = lc.left;
                //右树: 一直右
                right.push(rc);
                rc = rc.right;
            }
            if(lc == null && rc != null || lc != null && rc == null) return false;
            
            //左右树: 中 ()
            lc = left.pop();
            rc = right.pop();
            if(lc.val != rc.val) return false;

            //左树: 右
            lc = lc.right;
            //右树: 左
            rc = rc.left;   
        }
        return true;
    }    

}

Guess you like

Origin blog.csdn.net/qq_54773252/article/details/127171442
Recommended