-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslideno43.py
More file actions
41 lines (34 loc) · 963 Bytes
/
slideno43.py
File metadata and controls
41 lines (34 loc) · 963 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
# tree traveral
class Node:
def __init__(self, key):
self.left = None
self.right = None
self.val = key
def PostOrder(self, root):
if root:
self.PostOrder(root.left)
self.PostOrder(root.right)
print(root.val)
def PreOrder(self, root):
if root:
print(root.val)
self.PreOrder(root.left)
self.PreOrder(root.right)
def Inorder(self, root):
if root:
self.Inorder(root.left)
print(root.val)
self.Inorder(root.right)
root = Node("A")
root.left = Node("B")
root.right = Node("C")
root.left.left = Node("D")
root.left.right = Node("E")
root.right.left = Node("F")
root.right.right = Node("G")
print("Preorder traversal of binary tree is")
root.PreOrder(root)
print("\nInorder traversal of binary tree is")
root.Inorder(root)
print("\nPostorder traversal of binary tree is")
root.PostOrder(root)