Construct Tree from given Postorder and Inorder Traversals in C++

Construct Tree From Given Inorder and Postorder traversals in C++

There are three types of traversals in a tree: Inorder, Preorder and Postorder traversal. A tree can be formed with any two tree traversals in which one of them being the inorder traversal. In this article, we will learn how to construct a tree from given postorder and inorder traversals in C++.

This method is widely used in tree reconstruction problems where understanding the position of root and subtrees plays a key role. By using the properties of postorder traversal along with inorder indexing, we can efficiently rebuild the original binary tree structure.

Construct Tree from given Postorder and Inorder Traversals in C++

Algorithm For InOrder Traversal:

  1. Traverse The Left subtree.
  2. Print the node.
  3. Traverse the right subtree.

Algorithm For PostOrder Traversal:

  1. Traverse the left subtree.
  2. Traverse the right subtree.
  3. Print the node.
Construct Tree from given Inorder and Postorder traversals

Code Implementation for constructing tree from postorder and inorder traversals in C++

Code implementation for constructing a tree from postorder and inorder traversals in C++ involves recursively identifying the root from the postorder sequence and locating it in the inorder sequence to divide the tree into left and right subtrees. By repeating this process, the entire binary tree is reconstructed efficiently. This approach ensures correct tree formation by leveraging the properties of both traversals.

Run
#include<bits/stdc++.h>
using namespace std;
class Tree
{
public:
  int data;
  Tree *left = NULL, *right = NULL;
    Tree (int x)
  {
    data = x;
    left = NULL;
    right = NULL;
  }
};
int search (int inorder[], int start, int end, int element)
{
    int i = 0;
    for (i = start; i < end; i++) { if (inorder[i] == element) return i; } return i; } void printInorder (Tree * node) { if (node == NULL) return; printInorder (node->left);
  cout << node->data << " "; printInorder (node->right);
}

Tree *build_tree (int inorder[], int postorder[], int start, int end)
{
  static int index = end + 1;
  if (start > end)
    return NULL;
  Tree *curr_node = new Tree (postorder[index--]);
  int x = curr_node->data;
  if (start == end)
    return curr_node;

  int search_index = search (inorder, start, end, x);

  curr_node->right = build_tree (inorder, postorder, search_index + 1, end);

  curr_node->left = build_tree (inorder, postorder, start, search_index - 1);
  return curr_node;
}

int main ()
{
  int in[] = { 12, 25, 30, 37, 40, 50, 60, 62, 70, 75, 87 };
  int post[] = { 12, 30, 40, 37, 25, 60, 70, 62, 87, 75, 50 };
  Tree *root = build_tree (in, post, 0, 10);
  cout << "Inorder traversal\n";
  printInorder (root);
  return 0;
}


Output:

Inorder traversal
12 25 30 37 40 50 60 62 70 75 87

Explanation:

  • The Tree class defines a node structure with data, left, and right pointers, used to represent each node of the binary tree.
  • The search() function finds the index of a given element in the inorder array, which helps in dividing the tree into left and right subtrees.
  • The build_tree() function constructs the binary tree using inorder and postorder traversals, where the last element of postorder becomes the root node.
  • A static variable index is used to track the current element in the postorder array, moving from end to beginning during recursive calls.
  • The tree is built recursively by first constructing the right subtree and then the left subtree, based on the position of the root in the inorder array.

Time and Space Complexity:

OperationTime ComplexitySpace Complexity
search()O(n)O(1)
build_tree()O(n²)O(n)
printInorder()O(n)O(n)
Overall ComplexityO(n²)O(n)

Conclusion

Constructing a binary tree from inorder and postorder traversals highlights how traversal properties can be used to rebuild the original structure efficiently. By identifying the root from postorder and dividing the inorder array, the tree can be recursively formed in a systematic way.

This approach not only strengthens understanding of tree traversal concepts but also improves problem solving skills in recursion and data structures. It is a commonly asked concept in technical interviews and helps build a strong foundation for more advanced tree-based problems.

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

FAQs

Inorder traversal helps determine the left and right subtrees by locating the root position. Without it, we cannot uniquely identify the structure of the tree.

In postorder traversal, the last element always represents the root of the current subtree. This property is used repeatedly to build the tree recursively.

The time complexity is O(n) when using efficient lookup (like hashing for inorder indices). Each node is processed once during the construction.

Yes, a unique binary tree can be constructed if all node values are distinct. Duplicate values may lead to multiple possible tree structures.

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion : C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal Line by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric – C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree- CC++ | Java

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion :  C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal LIne by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric  C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree. CC++ | Java