Determine the in-order, pre-order, and post-order sequence of the following: 2 7 5 2 6 9 5 11 4 30 (20 (39 10 25 35 (42 15 23
Strange way to determine the tree. I printed in-order, pre-order, and post-order sequence of the "2 7 5 2 6 9 5 11 4 30". You can change it after the comment "Determinate graph". Good luck!
class Node:
def __init__(self, key):
self.left = None
self.right = None
self.val = key
def printInorder(root):
if root:
printInorder(root.left)
print(root.val),
printInorder(root.right)
def printPostorder(root):
if root:
printPostorder(root.left)
printPostorder(root.right)
print(root.val),
def printPreorder(root):
if root:
print(root.val),
printPreorder(root.left)
printPreorder(root.right)
# Determinate graph ------> 2 7 5 2 6 9 5 11 4 30
root = Node(2)
root.left = Node(7)
root.right = Node(5)
root.left.left = Node(2)
root.left.right = Node(6)
root.right.left = Node(9)
root.right.right = Node(5)
root.left.left.left = Node(11)
root.left.left.right = Node(4)
root.left.right.left = Node(30)
print("Preorder traversal of binary tree is")
printPreorder(root)
print("\nInorder traversal of binary tree is")
printInorder(root)
print("\nPostorder traversal of binary tree is")
printPostorder(root)
Comments
Leave a comment