-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathp4_3.py
More file actions
35 lines (26 loc) · 761 Bytes
/
p4_3.py
File metadata and controls
35 lines (26 loc) · 761 Bytes
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
from chapter_2.linked_list_utils import Node
from utils.graphs import BiNode, ltbt
def tree2LinkedList(root: BiNode):
q = []
ans = []
q.append((root, 0))
last_elem = None
last_seen_level = 1
while q:
ce, lvl = q.pop(0)
if lvl != last_seen_level:
ans.append(Node(ce.val))
last_seen_level = lvl
last_elem = ans[lvl]
else:
last_elem.next = Node(ce.val)
last_elem = last_elem.next
for child in (ce.left, ce.right):
if child is not None:
q.append((child, lvl+1))
return ans
if __name__ == "__main__":
extree = ltbt(list(range(10)))
resp = tree2LinkedList(extree)
for ll in resp:
print(ll)