给你一个二叉树的根节点 root
, 检查它是否轴对称。
示例 1:
输入:root = [1,2,2,3,4,4,3]
输出:true
示例 2:
输入:root = [1,2,2,null,3,null,3]
输出:false
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isSymmetric(self, root: Optional[TreeNode]) -> bool:
if not root:
return True
def dfs(left, right):
if not left and not right:
return True
if not left or not right:
return False
if left.val!=right.val:
return False
a=dfs(left.left, right.right)
b=dfs(left.right, right.left)
return a and b
# return dfs(left.left, right.right) and dfs(left.right, right.left)
res = dfs(root.left, root.right)
return res
递归
标签:right,return,val,dfs,二叉树,对称,101,root,left From: https://blog.51cto.com/u_16123878/6887743