-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path115.py
75 lines (71 loc) · 1.88 KB
/
115.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# This problem was asked by Google.
# Given two non-empty binary trees s and t, check whether tree t has exactly the
# same structure and node values with a subtree of s. A subtree of s is a tree
# consists of a node in s and all of this node's descendants. The tree s could also
# be considered as a subtree of itself.
# Example s:
# 7
# / \
# 5 1
# / \ \
# 4 3 1
# Example t:
# 1
# \
# 1
####
class Node:
def __init__(self, val = None, left = None, right = None):
self.left = left
self.right = right
self.val = val
n1 = Node(val = 4)
n2 = Node(val = 3)
n3 = Node(val = 1)
n4 = Node(left = n1, right = n2, val = 5)
n5 = Node(right = n3, val = 1)
s = Node(left = n4, right = n5, val = 7)
n6 = Node(val = 1)
t1 = Node(val = 1, right = n6)
t2 = Node(val = 7, right = t1)
####
# Once an preorder traversal is performed on both trees, it becomes a substring search problem.
def preorder(root):
if not root:
return []
# braces used for depth distinction
ret = ['(']
ret.append(root.val)
ret.extend(preorder(root.left))
ret.extend(preorder(root.right))
ret.append(')')
return ret
def KMP_preprocess(arr):
counter = 0
ret = [0]
for i in range(1, len(arr)):
if arr[counter] == arr[i]:
counter += 1
else:
counter = 0
ret.append(counter)
return ret
# it is possible to recurse this function over all nodes in s, the inorder step
# is performed to allow KMP.
def check_subtree(s, t):
S = preorder(s)
T = preorder(t)
preprocessed = KMP_preprocess(T)
ctr = 0
for c in S:
while c != T[ctr] and ctr != 0:
ctr = preprocessed[ctr - 1]
if c == T[ctr]:
ctr += 1
if ctr == len(T):
return True
return False
####
print(check_subtree(s, n4))
print(check_subtree(s, n5))
print(check_subtree(s, t2))