-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreeTraversal.py
More file actions
37 lines (31 loc) · 760 Bytes
/
Copy pathtreeTraversal.py
File metadata and controls
37 lines (31 loc) · 760 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
class Node:
def __init__(self, value):
self.left = None
self.right = None
self.val = value
def preorder(root):
if isinstance(root, Node):
print(root.val),
preorder(root.left)
preorder(root.right)
def postorder(root):
if isinstance(root, Node):
preorder(root.left)
preorder(root.right)
print(root.val)
def inoderorder(root):
if isinstance(root, Node):
preorder(root.left)
print(root.val)
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( "Preorder traversal of binary tree is")
preorder(root)
print( "Postorder traversal of binary tree is")
postorder(root)
print( "Inoder traversal of binary tree is")
inoderorder(root)