美文网首页
对称二叉树

对称二叉树

作者: 而立之年的技术控 | 来源:发表于2019-12-21 22:18 被阅读0次
微信图片_20191221221729.jpg
class Solution:
    def isSymmetric(self, root: TreeNode) -> bool:
        def compare(left, right):
            if left == None and right == None:
                return True
            if left == None:
                return False
            if right == None:
                return False
            if left.val != right.val:
                return False
            else:
                res1 = compare(left.left, right.right)
                res2 = compare(left.right, right.left)
            return res1 and res2
        if not root:
            return True
        else:
            res = compare(root.left, root.right)
        return res

相关文章

网友评论

      本文标题:对称二叉树

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