-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree1.py
More file actions
53 lines (41 loc) · 956 Bytes
/
Tree1.py
File metadata and controls
53 lines (41 loc) · 956 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
42
43
44
45
46
47
48
49
50
51
52
53
#Tree traveral in Python
class Node:
def __init__(self,val):
self.left=None
self.right=None
self.val =item
def inorder(root):
if root:
#Traverse left
inorder(root.left)
#Traverse right
print(str(root.val)+ "->",end='')
#Traverse right
inorder(root.right)
def postorder(root):
if root:
#Traverse left
postorder(root.left)
#Traverse right
postorder(root.right)
#Travserse root
print(str(root.val)+"->",end='')
def preorder(root):
if root:
#Traverse root
print(str(root.val)+'->',end='')
#Traverse left
preorder(root.left)
#Traverse right
preorder(root.right)
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
print("Inorder traversal")
inorder(root)
print('Preorder traversal')
preorder(root)
print('Post order traversal')
postorder(root)