【LeetCode OJ】Same Tree

时间:2023-03-09 15:24:51
【LeetCode OJ】Same Tree

Problem Link:

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

The following recursive version is accepted but the iterative one is not accepted...

# 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