#题目内容
给定两个二叉树,编写一个函数来检验它们是否相同。
如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
#解题思路
采用递归判断其左子树和右子树是否相等
class Solution:
def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
if p == None and q == None:
return True
elif q and p:
return p.val == q.val and self.isSameTree(p.left,q.left)
and self.isSameTree(q.right,p.right)
else:
return False