剑指 Offer 28. 对称的二叉树

剑指 Offer 28. 对称的二叉树


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

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

    public boolean Just(TreeNode rRoot,TreeNode lRoot){
        if(rRoot==null && lRoot==null){   //全为空,是镜像的
            return true;
        }
        if(rRoot==null || lRoot==null){   //一个为空 另一个不为空,则不是镜像的
            return false;
        }
        
        /*
            左子树和右子树数值相等 且 左子树的左和右子树的右 且 左子树的右,右子树的左
        */
        return rRoot.val==lRoot.val && Just(rRoot.left,lRoot.right)
        && Just(rRoot.right,lRoot.left);
    }
}

posted @ 2021-01-02 08:56  xiaoff  阅读(51)  评论(0编辑  收藏  举报