leetcode 每日一题 100.相同的树
递归
思路:
递归判断根节点,左子树,右子树是否相同。
代码:
# Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def isSameTree(self, p: TreeNode, q: TreeNode) -> bool: if not p and not q: return True if not q or not p: return False if p.val != q.val: return False return self.isSameTree(p.right, q.right) and \ self.isSameTree(p.left, q.left)