# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val
# Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def isSymmetric(self, root: TreeNode) -> bool: def recur(L,R): # 如果左右树都为空,则为遍历完的情况,返回True if not L and not R:return True # 一个为空一个不为空或者当前的值不同,返回False if not L or not R or L.val!=R.val:return False # 判断左子树的左边和右子树的右边是否相同,均相同才返回True return recur(L.left,R.right) and recur(L.right,R.left)
# 判断root是否为空,空则返回True,非空则递归判断子树情况 return recur(root.left,root.right) if root else True