题解 | #二叉树的下一个结点#
对称的二叉树
http://www.nowcoder.com/practice/ff05d44dfdb04e1d83bdbdab320efbcb
''' 递归 定义判断相等函数 三种情况:都为空 true;一个为空 false;值相等 true
'''
class TreeNode:
def init(self, x):
self.val = x
self.left = None
self.right = None
代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
@param pRoot TreeNode类
@return bool布尔型
class Solution: def isSame(self,root1,root2): if not root1 and not root2: return True if not root1 or not root2: return False return root1.val == root2.val and self.isSame(root1.left, root2.right) and self.isSame(root1.right, root2.left)
def isSymmetrical(self , pRoot: TreeNode) -> bool:
return self.isSame(pRoot, pRoot)
# write code here

查看20道真题和解析