-
Notifications
You must be signed in to change notification settings - Fork 40
/
list_of_depths.py
77 lines (66 loc) · 1.68 KB
/
list_of_depths.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
#!/usr/bin/python
# Date: 2020-10-21
#
# Description:
# Given a binary tree, design an algorithm which creates a linked list of all
# the nodes in each depth(for d level we will have d linked lists)
#
# Approach:
# Do tree level order traversal and add all nodes at a specific level to a
# new linked list
#
# Complexity:
# O(N) Time and space
import collections
class TreeNode:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
class LinkedListNode:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def traverse(self):
p = self.head
while p:
print(p.data, end=' ')
p = p.next
def add_end(self, data):
if self.head is None:
self.head = LinkedListNode(data)
return
p = self.head
while p.next:
p = p.next
p.next = LinkedListNode(data)
def get_list_of_depths(root):
if root is None:
return []
Q = collections.deque([root])
res = []
while Q:
count = len(Q)
linked_list = LinkedList()
while count:
count -= 1
node = Q.popleft()
linked_list.add_end(node.data)
if node.left:
Q.append(node.left)
if node.right:
Q.append(node.right)
res.append(linked_list)
return res
def main():
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.left = TreeNode(4)
for linked_list in get_list_of_depths(root):
linked_list.traverse()
print()
main()