-
Notifications
You must be signed in to change notification settings - Fork 154
/
level_order_tree.py
54 lines (42 loc) · 1.14 KB
/
level_order_tree.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
# This is a level order traversal of a
# a binary tree. It is also known as breadth
# first traversal.
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def printLevelOrder(root):
height = getHeight(root)
if root is None:
return
else:
# iteratively print all the levels
for i in range(1, height + 1):
printGivenLevel(root, i)
def printGivenLevel(root, level):
if root is None:
return
if level == 1:
print("%d" % root.data)
elif level > 1:
# recursively print the given level
printGivenLevel(root.left, level - 1)
printGivenLevel(root.right, level - 1)
def getHeight(root):
if root is None:
return 0
else:
lheight = getHeight(root.left)
rheight = getHeight(root.right)
if lheight > rheight:
return lheight + 1
else:
return rheight + 1
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
print("Level order traversal of binary tree is -")
printLevelOrder(root)