Same Tree

https://leetcode.com/problems/same-tree/

Solution 1: Recursion

Solution 2: Iteration

from collections import deque
 
def check(p, q) -> bool:
    if not p and not q:
        return True
    if not q or not p:
        return False
    if p.val != q.val:
        return False
    return True
 
deq = deque([(p, q)])
while deq:
    p, q = deq.popleft()
    if not check(p, q):
        return False
 
    if p:
        deq.append((p.left, q.left))
        deq.append((p.right, q.right))
 
return True

The key to make it work is append both children at the same time.