剑指 Offer 28. 对称的二叉树
作者:互联网
思路:递归的思想,当头节点不存在时直接返回True,递归遍历左右节点的值。
Python:
# 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 compare(root1,root2): if(not root1)and(not root2): return True if(not root1 or not root2)or(root1.val!=root2.val): return False return compare(root1.left,root2.right) and compare(root1.right,root2.left) return compare(root.left,root.right) if root else True
标签:compare,right,return,Offer,self,28,二叉树,root1,root2 来源: https://www.cnblogs.com/tiuyoaix/p/16167169.html