一 思路
最简单的迭代,判断存在不相同的情况,然后在相同情况下迭代,用and连接
二 代码
class Solution(object):
def isSameTree(self, p, q):
"""
:type p: TreeNode
:type q: TreeNode
:rtype: bool
"""
if (p and not q) or (q and not p):
return False
if not q and not p:
return True
if q and p:
if q.val==p.val:
return self.isSameTree(q.left,p.left) and self.isSameTree(q.right,p.right)
return False