LeetCode Symmetric Binary Tree (101 questions)

LeetCode Symmetric Binary Tree

@author:Jingdai
@date:2020.11.03

Title description (101 questions)

Given a binary tree, check whether it is mirror-symmetrical.

For example, the binary tree [1,2,2,3,4,4,3] is symmetric.

   1
  / \
 2   2
/ \ / \
3  4 4  3

Ideas

1. Recursive method

Insert picture description here

As shown in the figure, it can be seen that the left subtree and the right subtree of a symmetrical binary tree are mirror-symmetrical, while the left subtree of the left subtree is mirror-symmetrical with the right subtree of the right subtree, and the right subtree of the left subtree is mirrored and symmetrical. The tree is mirror-symmetrical to the left subtree of the right subtree... Isn't this a typical recursion problem? According to this property, it can be judged whether the two trees are mirror symmetry. Look at the code snippet below.

/*
 * determine whether the two trees are symmetric
 */
public boolean areSymmetric(TreeNode tree1, TreeNode tree2) {
    
    

    if (tree1 == null && tree2 == null) {
    
    
        return true;
    }
    if (tree1 != null && tree2 != null) {
    
    
        if (tree1.val == tree2.val) {
    
    
            return areSymmetric(tree1.left, tree2.right) 
                && areSymmetric(tree1.right, tree2.left);
        } else {
    
    
            return false;
        }
    }
    return false;
}

Then it is judged whether the tree is mirror-symmetrical. The judgment can be completed by judging whether the left subtree and the right subtree of a tree are mirror-symmetrical. The code snippet is as follows.

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

    return areSymmetric(root.left, root.right);
}

The entire code at the end is shown in the code section below.

2. Non-recursive method

Next we look at the non-recursive method. As shown in the figure below, the result of traversing the left subtree of the symmetric binary tree from left to right hierarchically is the same 1 2 3 4 5 6 7, and then the result of traversing the right subtree hierarchically from right to left is still 1 2 3 4 5 6 7.

Insert picture description here

It can be found that if a binary tree is mirror-symmetrical, the results of the left-to-right level traversal of the left subtree and the right-to-left level traversal of the right subtree are the same. Using this property, each time the left subtree is added to the queue in the order of traversal from left to right, and then the right subtree is added to the queue in the order of traversal from right to left. If If the binary tree is mirror-symmetrical, the two node values ​​are the same. If the two node values ​​are different, the binary tree must not be mirror-symmetrical and return directly; if the value of the last node is always the same when the level is traversed, it means The binary tree is a mirror-symmetrical binary tree. See the code section below for the code.

Code

1. Recursive method

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

    return areSymmetric(root.left, root.right);
}

/*
 * determine whether the two trees are symmetric
 */
public boolean areSymmetric(TreeNode tree1, TreeNode tree2) {
    
    

    if (tree1 == null && tree2 == null) {
    
    
        return true;
    }
    if (tree1 != null && tree2 != null) {
    
    
        if (tree1.val == tree2.val) {
    
    
            return areSymmetric(tree1.left, tree2.right) 
                && areSymmetric(tree1.right, tree2.left);
        } else {
    
    
            return false;
        }
    }
    return false;
}

2. Non-recursive method

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

    LinkedList<TreeNode> queue = new LinkedList<>();
    queue.offer(root.left);
    queue.offer(root.right);

    while (queue.size() != 0) {
    
    
        TreeNode tempLeftNode = queue.poll();
        TreeNode tempRightNode = queue.poll();
        if (tempLeftNode == null && tempRightNode == null) {
    
    
            continue;
        } else if (tempLeftNode == null || tempRightNode == null) {
    
    
            return false;
        } else {
    
    
            if (tempLeftNode.val == tempRightNode.val) {
    
    
                queue.offer(tempLeftNode.left);
                queue.offer(tempRightNode.right);
                queue.offer(tempLeftNode.right);
                queue.offer(tempRightNode.left);
            } else {
    
    
                return false;
            }
        }
    }
    return true;
}

Guess you like

Origin blog.csdn.net/qq_41512783/article/details/109480941