```
class Solution:
# @param p, a tree node
# @param q, a tree node
# @return a boolean
def isSameTree(self, p, q):
P = [p]
Q = [q]
while P and Q:
c_p = P.pop()
c_q = Q.pop()
if not c_p or not c_q:
if c_p != c_q:
return False
else:
continue
if c_p.val != c_q.val:
return False
P.append(c_p.left)
P.append(c_p.right)
Q.append(c_q.left)
Q.append(c_q.right)
if P or Q:
return False
return True
```