每日一道Leetcode - 剑指 Offer 28. 对称的二叉树【递归】
作者:互联网
# 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
标签:right,return,recur,Offer,root,self,28,二叉树,True 来源: https://blog.csdn.net/weixin_41041275/article/details/114738233