-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree_printer.py
77 lines (63 loc) · 1.77 KB
/
tree_printer.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
from collections import deque
class Node:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
def print_tree(root):
ret = []
temp = []
q = deque()
q.append(root)
last = root
next_last = None
current = None
while q:
current = q.popleft()
temp.append(current.val)
l_child, r_child = current.left, current.right
if l_child is not None:
q.append(l_child)
next_last = l_child
if r_child is not None:
q.append(r_child)
next_last = r_child
if current is last:
ret.append(temp)
temp = []
last = next_last
return ret
def serialize(node, ret):
if node is None:
ret.append('#')
ret.append('!')
return
val = node.val
val_chars = str(val).split()
ret.extend(val_chars)
ret.append('!')
serialize(node.left, ret)
serialize(node.right, ret)
def deserialize(chars, start=0):
"""
['1','2','!','3','!','#','!','#','!','#','!']
"""
# base case
if len(chars) <= start:
return None, start
end = chars.index('!', start)
if chars[end - 1] != '#':
val = int(''.join(chars[start: end]))
node = Node(val)
node.left, start = deserialize(chars, end + 1)
node.right, start = deserialize(chars, start)
return node, start
else:
return None, end + 1
if __name__ == '__main__':
chars = ['1', '2', '!', '3', '!', '#', '!', '#', '!', '#', '!']
tree, _ = deserialize(chars)
assert print_tree(tree) == [[12], [3]], 'print_tree error'
chars_serialized = []
serialize(tree, chars_serialized)
assert chars == chars_serialized, 'error'