leetcode101:对称二叉数

class Solution:
    def isSymmetric(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        def helper(root, mirror):
            if not root and not mirror:
                return True
            if root and mirror and root.val == mirror.val:
                return helper(root.left, mirror.right) and helper(root.right, mirror.left)
            return False
        return helper(root, root)

将二叉树root复制为二叉树root和二叉树mirror,然后比较root.val and mirror.val 以及root.left and mirror.right 以及 root.right and mirror.left 是否相等,若全部相等则return ture反之return false

猜你喜欢

转载自blog.csdn.net/weixin_43160613/article/details/83054480