这题嘛,想到和 isSameTree 很像,没想到拆成两个函数,只想到放一个函数解决。实际递归遍历调 isSameTree 就 ok 了。
Definition for a binary tree node.
class TreeNode(object):
def init(self, x):
self.val = x
self.left = None
self.right = None
class Solution(object):
def isSubtree(self, s, t):
"""
:type s: TreeNode
:type t: TreeNode
:rtype: bool
"""
if not s:
return False
if self.isSameTree(s, t):
return True
return self.isSubtree(s.left, t) or self.isSubtree(s.right, t)
def isSameTree(self, s, t):
if not t and not s:
return True
elif (not t) or (not s) or (s.val != t.val):
return False
else:
return self.isSameTree(s.left, t.left) and self.isSameTree(s.right, t.right)
网友评论