递归法
class Solution:
def isSymmetric(self, root: TreeNode) -> bool:
if not root:
return True
def twoSym(left, right):
if not left and not right:
return True
if (left and not right) or (not left and right):
return False
return left.val == right.val and twoSym(left.right, right.left) and twoSym(left.left, right.right)
return twoSym(root.left, root.right)