题目链接:https://leetcode-cn.com/problems/dui-cheng-de-er-cha-shu-lcof/
难度:简单
描述:
请实现一个函数,用来判断一棵二叉树是不是对称的。如果一棵二叉树和它的镜像一样,那么它是对称的。
题解
# 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 recursion(l, r):
if l is None and r is None:
return True
if l is None or r is None or l.val != r.val:
return False
return recursion(l.left, r.right) and recursion(l.right, r.left)
if root is None:
return True
return recursion(root.left, root.right)