(Java refers to offer) symmetrical binary tree

1. Question analysis

Please implement a function to determine whether a binary tree is symmetric. Note that if a binary tree is the same as the mirror image of this binary tree, define it as symmetric.

To judge whether a binary tree is symmetrical, mainly to judge whether the left subtree and the right subtree of the binary tree are symmetrical.

(1) If the root node of the binary tree is empty, it meets the condition
(2) Recursively judge whether the left node of the left subtree of the binary tree and the right node of the right subtree, and the right node of the left subtree and the left node of the right subtree are equal .

Two, recursive code

class TreeNode {
    
    
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) 
    {
    
    
        this.val =val;
    }
}
public class Test_15 {
    
    

    boolean isSymmetrical(TreeNode pRoot) {
    
    
        //二叉树为空
        if (pRoot == null) {
    
    
            return true;
        }
        return isSymmetrical(pRoot.left, pRoot.right);
    }

    private boolean isSymmetrical(TreeNode left, TreeNode right) {
    
    
        //左右节点均为空
        if (left == null && right == null) {
    
    
            return true;
        }
        //左右节点有一个为空
        if (left == null || right == null) {
    
    
            return false;
        }
        //递归判断
        return left.val == right.val
                && isSymmetrical(left.left, right.right)
                && isSymmetrical(left.right, right.left);
    }
}

3. Non-recursive code: DFS depth-first traversal

DFS uses stack to store pairs of nodes

(1) When popping out of the stack, it needs to be in pairs.
(2) If both are empty, continue
(3) If one is empty, return false directly, otherwise it is not empty. Compare whether the current value is equal.
(4) Each stack needs to be paired: the left node of the left subtree and the right node of the right subtree; the right node of the left subtree and the left node of the right subtree;

 boolean isSymmetricalDFS(TreeNode pRoot)
    {
    
    
        if(pRoot == null) return true;
        Stack<TreeNode> s = new Stack<>();
        s.push(pRoot.left);
        s.push(pRoot.right);
        while(!s.empty()) {
    
    
            TreeNode right = s.pop();//成对取出
            TreeNode left = s.pop();
            if(left == null && right == null) continue;
            if(left == null || right == null) return false;
            if(left.val != right.val) return false;
            //成对插入
            s.push(left.left);
            s.push(right.right);
            s.push(left.right);
            s.push(right.left);
        }
        return true;
    }

Fourth, non-recursive code: BFS breadth-first traversal

BFS uses Queue to store pairs of nodes

(1) Depart in pairs
(2) All are empty, continue to judge, one party is empty and return false
(3) Similarly, every time you enter the team, you must pair: the left node of the left subtree and the right node of the right subtree; left; The right node of the subtree and the left node of the right subtree;

 boolean isSymmetricalBFS(TreeNode pRoot)
    {
    
    
        if(pRoot == null) return true;
        Queue<TreeNode> s = new LinkedList<>();
        s.offer(pRoot.left);
        s.offer(pRoot.right);
        while(!s.isEmpty()) {
    
    
            TreeNode left= s.poll();//成对取出
            TreeNode right= s.poll();
            if(left == null && right == null) continue;
            if(left == null || right == null) return false;
            if(left.val != right.val) return false;
            //成对插入
            s.offer(left.left);
            s.offer(right.right);
            s.offer(left.right);
            s.offer(right.left);
        }
        return true;
    }

Guess you like

Origin blog.csdn.net/nanhuaibeian/article/details/108742442