forked from ravikartar/hacktober2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree_traversal_inorder.py
More file actions
41 lines (31 loc) · 909 Bytes
/
Tree_traversal_inorder.py
File metadata and controls
41 lines (31 loc) · 909 Bytes
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
# Program for inorder tree traversal
# 1
# 2 3
# 4 5 6 7
# A class that represents an individual node in a binary tree
class Node:
def __init__(self, key):
self.left = None
self.right = None
self.val = key
# A function to print inorder tree traversal
def printInorder(root):
if root:
# First recur on left child of the root
printInorder(root.left)
# Print the data of node
print(root.val, end=' ')
# Now recur on the right child of the root
printInorder(root.right)
# Driver Code
if __name__ == "__main__":
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
root.right.left = Node(6)
root.right.right = Node(7)
print("Inorder traversal of binary tree is:")
printInorder(root)
print()