class Solution:
def isSubtree(self, s: TreeNode, t: TreeNode) -> bool:
# both None
if s is None and t is None:
return True
# any None
if s is None or t is None:
return False
# if same return
if self.is_same(s, t):
return True
# recursive
return self.isSubtree(s.left, t) or self.isSubtree(s.right, t)
def is_same(self, s, t):
# both None
if s is None and t is None:
return True
# any None
if s is None or t is None:
return False
# compare s.val and t.val
if s.val != t.val:
return False
return self.is_same(s.left, t.left) and self.is_same(s.right, t.right)
leetcode572 Subtree of Another Tree 子树问题
猜你喜欢
转载自blog.csdn.net/weixin_36149892/article/details/97615920
今日推荐
周排行