8min,没有调试一次通过
class Solution:
def isSubtree(self, s: TreeNode, t: TreeNode) -> bool:
if not s:return False
if s.val==t.val and self.isSame(s,t):return True
return self.isSubtree(s.left,t) or self.isSubtree(s.right,t)
def isSame(self,root1:TreeNode,root2:TreeNode)->bool:
if not root1 and not root2:return True
if not root1 or not root2:return False
if root1.val!=root2.val:return False
return self.isSame(root1.left,root2.left) and self.isSame(root1.right,root2.right)