题目描述:
方法一:DFS枚举 O(s*t) O(max(ds,dt) d代表深度
class Solution: def isSubtree(self, s: TreeNode, t: TreeNode) -> bool: def isSameTree(s, t): if not s and not t: return True if not s or not t: return False return s.val == t.val and isSameTree(s.left, t.left) and isSameTree(s.right, t.right) if not s and not t: return True if not s or not t: return False return isSameTree(s, t) or self.isSubtree(s.left, t) or self.isSubtree(s.right, t)
方法二:dfs转序列 + 判断子串算法(kmp等)O(s+t)
原文:https://www.cnblogs.com/oldby/p/12843735.html