```
def isSameTree(self, p,q):
if not p and not q:
# two empty roots
return True
elif (not p and q) or (not q and p):
# one node is empty but another one is not
return False
elif p.val != q.val:
return False
else:
# p and q are equal
return (self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right))
```