Sword refers to offer 28. Symmetrical binary tree

Sword refers to offer 28. Symmetrical binary tree

Title description

Insert picture description here

Problem-solving ideas

class Solution {
    
    
    public boolean isSymmetric(TreeNode root) {
    
    
        if (root == null) return true;
        return isSymmetric(root.left, root.right);
    }

    //判断 A 和 B 是否是对称的
    public boolean isSymmetric(TreeNode A, TreeNode B) {
    
    
        //若A、B均遍历完了,说明是对称的
        if (A == null && B == null) return true;
        //若有一方没遍历完,或者两者不相等,说明不对称
        if (A == null || B == null || A.val != B.val) return false;
        //继续判断子树是否对称
        return isSymmetric(A.left, B.right) && isSymmetric(A.right, B.left);
    }
}

Guess you like

Origin blog.csdn.net/cys975900334/article/details/115049100