-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathlevel_order_tree_traversal_using_queue.py
61 lines (48 loc) · 1.11 KB
/
level_order_tree_traversal_using_queue.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
# Date: 2020-09-22
#
# Description:
# Level order traversal of tree
#
# Approach:
# Uses DFS traversal approach.
# 1. Prints current node data.
# 2. enqueue's both left and right child.
# 3. dequeue a node from queue.
# 4. Execute while queue is not empty.
#
# Reference:
# https://www.geeksforgeeks.org/?p=2686
#
# Complexity:
# Time: O(n), n is number of node in tree
# Space: O(n), Requires extra space to maintain queue
import collections
class Node:
def __init__(self, v):
self.data = v
self.left = None
self.right = None
def level_order_traversal(root):
Q = collections.deque()
Q.append(root)
while Q:
node = Q.popleft()
if node.left:
Q.append(node.left)
if node.right:
Q.append(node.right)
print(node.data, end=' ')
print()
def main():
root = Node(1);
root.left = Node(2);
root.right = Node(3);
root.left.left = Node(4);
root.left.right = Node(5);
root.left.left.left = Node(6);
level_order_traversal(root)
if __name__ == '__main__':
main()
# Output
# -----------------
# 1 2 3 4 5 6