Question Link:
https://leetcode.com/problems/same-tree/
No comments… straightforward to implement. The following recursive version is accepted but the iterative version is not…
[-]
Python code accepted by LeetCode OJ
# Definition for a binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# @param p, a tree node
# @param q, a tree node
# @return a boolean
def isSameTree(self, p, q):
"""
Check both trees level by level using BFS
"""
if not p or not q:
return p == q
if p.val == q.val:
return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
else:
return False