美文网首页
101. Symmetric Tree

101. Symmetric Tree

作者: 苏州城外无故人 | 来源:发表于2019-03-01 14:16 被阅读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;
    }

相关文章

网友评论

      本文标题:101. Symmetric Tree

      本文链接:https://www.haomeiwen.com/subject/sesduqtx.html