给你一个二叉树的根节点 root
, 检查它是否轴对称。
示例 1:
输入:root = [1,2,2,3,4,4,3]
输出:true
示例 2:
输入:root = [1,2,2,null,3,null,3]
输出:false
代码:
# [101] 对称二叉树
#
# @lc code=start
# 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: TreeNode) -> bool:
def isEqual(l,r):
if l==None and r==None:
return
if l==None and r!=None:
return False
if l!=None and r==None:
return False
if l.val==r.val:
if isEqual(l.left,r.right)==False or isEqual(l.right,r.left)==False:
return False
else:
return False
return True if isEqual(root.left,root.right)==None else False
# @lc code=end