In: Computer Science
For the above program, we need to have the following things into consideration.
Following is the python code:
# Printing the post order
post_order = []
def postorder(inorder, preorder, n):
if preorder[0] in inorder:
root = inorder.index(preorder[0])
if root != 0: # left subtree exists
postorder(inorder[:root],
preorder[1:root + 1],
len(inorder[:root]))
if root != n - 1: # right subtree exists
postorder(inorder[root + 1:],
preorder[root + 1:],
len(inorder[root + 1:]))
post_order.append(preorder[0])
def get_preorder(inorder, preorder):
postorder(inorder, preorder, len(inorder))
# Driver Code
inorder = [4, 2, 5, 1, 3, 6];
preorder = [1, 2, 4, 5, 3, 6];
n = len(inorder)
print("Postorder traversal ")
get_preorder(inorder, preorder)
print(post_order)
Following is the snippet of the above program .:

The Output of the above program.

That was a nice
question to answer
Friend, If you have any doubts in understanding do let me know in
the comment section. I will be happy to help you further.
Please like if you think effort deserves like.
Thanks