# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def isSymmetric(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if not root:
return True
def dfs(left,right):
if left is None and right is None:
return True
elif not (left and right):
return False
elif left.val == right.val:
# 是关键,对称的条件:左子树的左节点和右子树的右节点、左子树右节点与右子树左节点要相同
return dfs(left.left,right.right) and dfs(left.right,right.left)
else:
return False
return dfs(root.left,root.right)