Construct a tree from Inorder and Level order traversals | Set 2

Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree. Following is an example to illustrate the problem.


Input: Two arrays that represent Inorder
       and level order traversals of a 
       Binary Tree
in[]    = {4, 8, 10, 12, 14, 20, 22};
level[] = {20, 8, 22, 4, 12, 10, 14};

Output: Construct the tree represented 
        by the two arrays.
        For the above two arrays, the 
        constructed tree is shown.

We have discussed a solution in below post that works in O(N^3)
Construct a tree from Inorder and Level order traversals | Set 1

Approach : Following algorithm uses O(N^2) time complexity to solve the above problem using the unordered_set data structure in c++ (basically making a hash-table) to put the values of left subtree of the current root and later and we will check in O(1) complexity to find if the current levelOrder node is part of left subtree or not.

If it is the part of left subtree then add in one lLevel arrray for left other wise add it to rLevel array for right subtree.

Below is the c++ implementation with the above idea





/* program to construct tree using inorder 
   and levelorder traversals */
#include <iostream>
using namespace std;
/* A binary tree node */
struct Node
    int key;
    struct Node* left, *right;
Node* makeNode(int data){
    Node* newNode = new Node();
    newNode->key = data;
    newNode->right = newNode->right = NULL;
    return newNode;
// Function to build tree from given 
// levelorder and inorder
Node* buildTree(int inorder[], int levelOrder[],
                int iStart, int iEnd, int n)
    if (n <= 0) 
          return NULL;
    // First node of level order is root
    Node* root = makeNode(levelOrder[0]);
    // Search root in inorder
    int index = -1;
    for (int i=iStart; i<=iEnd; i++){
        if (levelOrder[0] == inorder[i]){
            index = i;
    // Insert all left nodes in hash table
    unordered_set<int> s;
    for (int i=iStart;i<index;i++)
    // Separate level order traversals
    // of left and right subtrees.
    int lLevel[s.size()];  // Left 
    int rLevel[iEnd-iStart-s.size()]; // Right
    int li = 0, ri = 0;
    for (int i=1;i<n;i++) {
        if (s.find(levelOrder[i]) != s.end())
            lLevel[li++] = levelOrder[i]; 
            rLevel[ri++] = levelOrder[i];        
    // Recursively build left and right
    // subtrees and return root.
    root->left = buildTree(inorder, lLevel, 
                 iStart, index-1, index-iStart);
    root->right = buildTree(inorder, rLevel, 
                  index+1, iEnd, iEnd-index);
    return root;
/* Utility function to print inorder 
traversal of binary tree */
void printInorder(Node* node)
    if (node == NULL)
    cout << node->key << " ";
// Driver Code 
int main()
    int in[] = {4, 8, 10, 12, 14, 20, 22};
    int level[] = {20, 8, 22, 4, 12, 10, 14};
    int n = sizeof(in)/sizeof(in[0]);
    Node *root = buildTree(in, level, 0,
                           n - 1, n);
    /* Let us test the built tree by 
     printing Insorder traversal */
    cout << "Inorder traversal of the "
            "constructed tree is \n";
    return 0;

Output :

Inorder traversal of the
constructed tree is 4 8 10 12 14 
20 22 

Time Complexity: O(N^2)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : siddhartha_agarwal

Article Tags :
Practice Tags :