101. Symmetric Tree

2019-03-01  本文已影响0人  苏州城外无故人
image.png

思路:递归,左子树和右子树是否相等


 public boolean isSymmetric(TreeNode root) {
        return isMirror(root, root);
    }

    public boolean isMirror(TreeNode t1, TreeNode t2) {
        if (t1 == null && t2 == null) {
            return true;
        }
        if (t1 == null || t2 == null) {
            return false;
        }
        if (t1.val == t2.val) {
            return isMirror(t1.left, t2.right) && isMirror(t1.right, t2.left);
        }
        return false;
    }
上一篇 下一篇

猜你喜欢

热点阅读