-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcodechallenge_003.py
153 lines (124 loc) · 3.18 KB
/
codechallenge_003.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
'''
Date: 12/02/2018
Problem description:
===================
This problem was asked by Google.
Given the root to a binary tree, implement serialize(root), which serializes the tree into a string,
and deserialize(s), which deserializes the string back into the tree.
For example, given the following Node class
class Node:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
The following test should pass:
node = Node('root', Node('left', Node('left.left')), Node('right'))
assert deserialize(serialize(node)).left.left.val == 'left.left'
Pseudo code:
============
1. Construct the __init__ with self, val, left, right
2. implement the Insert function
3. implement the serialize(root), where root is the initialized node
4. implement the deserialize(s) function where s is a string
The input for val has the format of 'root, 'root.left.right' or 'left.left.right.left'
so, it is nicer to split the string
e.g.
str = 'root.left.right'
arr = str.split('.')
(*) Oh and this string should fail: 'left.left.right.left' because this is a rooted binary tree.
'''
class Node:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
def insert(self, val, left, right):
if self.left is None:
self.left = Node(val)
else:
self.left.insert(val)
if self.right is None:
self.right = Node(val)
else:
self.right.insert(val)
'''
serializes the tree (root) into a string (s)
treat root as a Node data structure.
'''
def serialize(root):
# check edge case
if root is None:
return None
# base case
if root.val == 'root':
arr.append(root.val)
arr.append('.')
# add root node
arr.append(node.val)
# add left node
serialize(root.left, arr)
# add right node
serialize(root.right, arr)
'''
deserializes the string back into the tree
'''
def deserialize(s):
# We use list.pop() and so we need to use this wrapper to
# first convert the string to an array and
# reverse the array before sending it to _deserialize()
# check edge case
if (len(s) == 0):
return None
# split the string into array
arr = str(s).split('.')
arr.reverse()
print(arr)
do_deserialize(arr)
def do_deserialize(arr):
if len(arr) > 0:
val = arr.pop()
# base case
if val == 'root':
# create a new node as the root node
node = Node('root', None, None)
else:
node = Node(arr)
# add root
node.val = val
# add left
node.left = do_deserialize(arr)
# add right
node.right = do_deserialize(arr)
# go up the node
print(type(node))
return node
if __name__ == "__main__":
s = 'root.left.left'
a = s.split('.')
print(s)
print(a)
deserialize(s)
newstr = serialize(deserialize(s))
print(newstr)
'''
tree = Node('root', Node('left', Node('left.left')), Node('right'))
print(tree.val)
print(tree.left)
print(tree.right)
'''
'''
Run-time output:
===============
markn@raspberrypi3:~/devel/py-src/DailyCodeChallenge $ python codechallenge-03.py
root.left.left
['root', 'left', 'left']
['left', 'left', 'root']
<type 'instance'>
<type 'instance'>
<type 'instance'>
['left', 'left', 'root']
<type 'instance'>
<type 'instance'>
<type 'instance'>
None
'''