题目描述
请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。1 class Solution: 2 def isSymmetrical(self, pRoot): 3 # write code here 4 def mirror(left,right): 5 if left == None and right == None: 6 return True 7 elif left == None or right == None: 8 return False 9 if left.val != right.val: 10 return False 11 ret1 = mirror(left.left,right.right) 12 ret2 = mirror(left.right,right.left) 13 return ret1 and ret2 14 if pRoot == None: 15 return True 16 return mirror(pRoot.left,pRoot.right)
2020-01-01 09:32:41