Lc101_对称二叉树

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

    /**
     * 优化是对于if判断,本来三个if判断,优化为俩个效率从1ms降到0ms
     * @param left
     * @param right
     * @return
     */
    static boolean isSame(TreeNode left, TreeNode right) {
        if (left == null && right != null) {
            return false;
        }  else if (left == null|| right == null) {
            return true;
        }

        return left.val == right.val &&isSame(left.left, right.right) && isSame(left.right, right.left);
    }
posted @ 2021-04-15 10:53  小傻孩丶儿  阅读(55)  评论(0编辑  收藏  举报