1class Solution:2def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:3stack = [[p, q]]4while stack:5p, q = stack.pop()6if not p and not q: # (1)7continue8elif p and q and p.val == q.val: # (2)9stack.append([p.left, q.left])10stack.append([p.right, q.right])11else: # (3)12return False13return True