1 终止条件:if not p and not q
2 返回结果:self.isSameTree(p.left, q.left) 和self.isSameTree(p.right, q.right)
3 如果这棵树的根节点正确,左右子树也正确,那么返回True,
4 如果遍历过程中发现任何错误,返回False
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
if not p and not q:
return True
if not p and q or not q and p:
return False
if p and q:
if p.val != q.val:
return False
return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)