58.对称的二叉树(python)

题目描述

请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。
class Solution:
    def isSymmetrical(self, pRoot):
        # write code here
        def mirror(left,right):
            if left == None and right == None:
                return True
            elif left == None or right == None:
                return False
            if left.val != right.val:
                return False
            ret1 = mirror(left.left,right.right)
            ret2 = mirror(left.right,right.left)
            return ret1 and ret2
        if pRoot == None:
            return True
        return mirror(pRoot.left,pRoot.right)

2020-01-01 09:32:41