-
Notifications
You must be signed in to change notification settings - Fork 0
/
107.py
57 lines (42 loc) · 1.06 KB
/
107.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
"""
Problem:
Print the nodes in a binary tree level-wise. For example, the following should print
1, 2, 3, 4, 5.
1
/ \
2 3
/ \
4 5
"""
from typing import List
from DataStructures.Queue import Queue
from DataStructures.Tree import BinaryTree, Node
def get_lvl_wise_nodes(tree: BinaryTree) -> List[Node]:
# using bfs to generate the list of nodes by level
if not tree.root:
return []
queue = Queue()
queue.enqueue(tree.root)
ans = []
while not queue.is_empty():
node = queue.dequeue()
if node.left is not None:
queue.enqueue(node.left)
if node.right is not None:
queue.enqueue(node.right)
ans.append(node.val)
return ans
if __name__ == "__main__":
tree = BinaryTree()
tree.root = Node(1)
tree.root.left = Node(2)
tree.root.right = Node(3)
tree.root.right.left = Node(4)
tree.root.right.right = Node(5)
print(f"Tree: {tree}")
print(f"Level wise result: {get_lvl_wise_nodes(tree)}")
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(n)
"""