0
Construct the binary tree for the inorder and post order traversal sequence given below: -

Inorder “INFORMATION”

Postorder “INOFMAINOTR”

Write a function to traverse a tree in postOrder

binary search tree • 1.7k  views
0
0

Binary tree construction:

Since, R is the last element in the post order sequence, R will be the root.

Elements to the left of R will be a part of left sub tree and the elements to the right of R will be a part of right sub tree.

Further, F is the last element of post Order sequence in the left sub tree.

Hence, F will be the root element in this case.

Elements before F i.e. I and N will be a part of left sub tree and O will be a part of right sub tree.

This process can be continued for the remaining elements to obtain the resultant binary tree.

The steps mentioned above can be represented graphically as follows:

C++ function to traverse a tree in postOrder

voidBinarySearchTree::postOrder(node *ptr)
{
if (root == NULL)
{
cout<<"Tree is empty."<<endl;
return;
}
if (ptr != NULL)
{
postOrder(ptr->left);
postOrder(ptr->right);
cout<<ptr->info<<"  ";
}
}

0
Please log in to add an answer.

#### Continue reading

Find answer to specific questions by searching them here. It's the best way to discover useful content.