-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lnorder.py
73 lines (52 loc) · 1.52 KB
/
Lnorder.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
class Node:
# A utility function to create a new node
def __init__(self, key):
self.data = key
self.left = None
self.right = None
# Function to print level order traversal of tree
def printLevelOrder(root):
h = height(root)
for i in range(1, h + 1):
printGivenLevel(root, i)
# Print nodes at a given level
def printGivenLevel(root, level):
if root is None:
return
if level == 1:
print("%d" % (root.data), )
elif level > 1:
printGivenLevel(root.left, level - 1)
printGivenLevel(root.right, level - 1)
def height(node):
if node is None:
return 0
else:
# Compute the height of each subtree
lheight = height(node.left)
rheight = height(node.right)
# Use the larger one
if lheight > rheight:
return lheight + 1
else:
return rheight + 1
# Driver program to test above function
root = Node(2)
root.left = Node(1)
root.right = Node(4)
root.right.left = Node(3)
root.right.right = Node(5)
# root.left.left = Node(1)
# root.left.right = Node(3)
# root.right.left.right = Node(3)
# root.right.left.left = Node(1)
# root.right.right.right = Node(10)
# root.right.right.left = Node(7)
# root.left.left.right = Node(6)
# root.left.left.left = Node(6)
# root.left.right.right = Node(6)
# root.left.right.left = Node(6)
# root.right.right.right.right = Node(11)
# root.right.right.right.left = Node(9)
print("Level order traversal of tree is -")
printLevelOrder(root)