# 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.

Examples:

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> ` `#include<set> ` `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) r ` ` ` `eturn 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; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Insert all left nodes in hash table ` ` ` `unordered_set<` `int` `> s; ` ` ` `for` `(` `int` `i=iStart;i<index;i++) ` ` ` `s.insert(inorder[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]; ` ` ` `else` ` ` `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) ` ` ` `return` `; ` ` ` `printInorder(node->left); ` ` ` `cout << node->key << ` `" "` `; ` ` ` `printInorder(node->right); ` `} ` ` ` `// 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"` `; ` ` ` `printInorder(root); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output :

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

Time Complexity: O(N^2)

## Recommended Posts:

- Construct a tree from Inorder and Level order traversals | Set 1
- Construct Tree from given Inorder and Preorder traversals
- Calculate height of Binary Tree using Inorder and Level Order Traversal
- Construct a complete binary tree from given array in level order fashion
- Tree Traversals (Inorder, Preorder and Postorder)
- Check if given Preorder, Inorder and Postorder traversals are of same tree
- Check if given Preorder, Inorder and Postorder traversals are of same tree | Set 2
- Construct BST from its given level order traversal | Set-2
- Construct BST from its given level order traversal
- Construct Full Binary Tree from given preorder and postorder traversals
- Construct a Binary Tree from Postorder and Inorder
- Construct Special Binary Tree from given Inorder traversal
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- Flatten Binary Tree in order of Level Order Traversal
- Insertion in n-ary tree in given order and Level order traversal

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.