请实现一个函数,用来判断一棵二叉树是不是对称的。如果一棵二叉树和它的镜像一样,那么它是对称的。
class Solution:
def isSymmetric(self, root: TreeNode) -> bool:
def recur(l,r):
if not l and not r: return True
if not l or not r or l.val != r.val:
return False
return recur(l.left,r.right) and recur(l.right,r.left)
if root:
return recur(root.left,root.right)
else:
return True