# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
stack = [(p, q)]
while stack:
p, q = stack.pop()
if not p and not q:
continue
if p and q and p.val == q.val:
stack.append((p.left, q.left))
stack.append((p.right, q.right))
else:
return False
return True
leetcode刷题python之相同的树
最新推荐文章于 2022-03-18 12:45:23 发布