题解 | #对称的二叉树#
对称的二叉树
https://www.nowcoder.com/practice/ff05d44dfdb04e1d83bdbdab320efbcb?tpId=295&tqId=23452&ru=/exam/oj&qru=/ta/format-top101/question-ranking&sourceUrl=%2Fexam%2Foj
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param pRoot TreeNode类 # @return bool布尔型 # class Solution: def isSymmetrical(self , pRoot: TreeNode) -> bool: if not pRoot: return True # write code here def compare( left, right) -> bool: if not left and not right: return True if (not right and left) or (not left and right): return False if left.val != right.val: return False if left.val == right.val: A = compare(left.right,right.left) B = compare(left.left,right.right) return A and B else: return False return compare(pRoot.left,pRoot.right)