代码思路:采用递归思想,逐级判断树节点是否相等或为空,搜索完毕即可得到最终答案
class Solution:
def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
def cheak(p, q):
if p == None and q == None:
return True
if p == None or q == None:
return False
if p.val != q.val:
return False
return cheak(p.left, q.left) and cheak(p.right, q.right)
return cheak(p,q)