-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
572.py
44 lines (39 loc) · 1.47 KB
/
572.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
__________________________________________________________________________________________________
sample 60 ms submission
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def isSubtree(self, s: TreeNode, t: TreeNode) -> bool:
def dfs(a,b):
if not a or not b:
return not a and not b
if a.val==b.val and dfs(a.left,b.left) and dfs(a.right,b.right):
return True
if b is t:
return dfs(a.left,t) or dfs(a.right,t)
return dfs(a,t)
return dfs(s,t)
__________________________________________________________________________________________________
sample 14252 kb submission
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def isMatch(self, s, t):
if not(s and t):
return s is t
return (s.val == t.val and
self.isMatch(s.left, t.left) and
self.isMatch(s.right, t.right))
def isSubtree(self, s, t):
if self.isMatch(s, t): return True
if not s: return False
return self.isSubtree(s.left, t) or self.isSubtree(s.right, t)
__________________________________________________________________________________________________