The idea is to construct the root node from the first element of the level order array. Find the index of this element in the in-order array.Recursively create the left subtree from the elements present on the left side to the current element in the in-order array. Similarly, create the right subtree from the elements present on the right side to the current element in the in-order array.
Below is the implementation of the above approach:
C++
// c++ program to construct tree using // inorder and levelorder traversals#include<bits/stdc++.h>usingnamespacestd;classNode{public:intkey;Node*left,*right;Node(intx){key=x;left=nullptr;right=nullptr;}};// Function to find the index of an element.intsearchValue(vector<int>in,intvalue,ints,inte){for(inti=s;i<=e;i++){if(in[i]==value)returni;}return-1;}// Recursive function to build the binary tree.Node*buildTreeRecur(vector<int>in,vector<int>level,ints,inte){// For empty array, return nullif(s>e){returnnullptr;}// create the root NodeNode*root=newNode(level[0]);// find the index of first element of level array// in the in-order array.intindex=searchValue(in,level[0],s,e);intlCnt=index-s,rCnt=e-index;// Level order array for left and right subtree.vector<int>lLevel(lCnt);vector<int>rLevel(rCnt);// add the left and right elements to lLevel and // rLevelintl=0,r=0;for(inti=1;i<e-s+1;i++){intj=searchValue(in,level[i],s,e);// If the value is present in// left subtree.if(j<index)lLevel[l++]=level[i];// else it will be present in // right subtree.elserLevel[r++]=level[i];}// Recursively create the left and right subtree.root->left=buildTreeRecur(in,lLevel,s,index-1);root->right=buildTreeRecur(in,rLevel,index+1,e);returnroot;}Node*buildTree(vector<int>inorder,vector<int>level,intn){// Build the tree recursively.Node*root=buildTreeRecur(inorder,level,0,n-1);returnroot;}voidprintInorder(Node*head){Node*curr=head;if(curr==nullptr)return;printInorder(curr->left);cout<<curr->key<<" ";printInorder(curr->right);}intmain(){vector<int>in={4,8,10,12,14,20,22};vector<int>level={20,8,22,4,12,10,14};intn=level.size();Node*root=buildTree(in,level,n);printInorder(root);return0;}
Java
// Java Program to construct tree using // inorder and levelorder traversalsimportjava.util.*;classNode{intkey;Nodeleft,right;Node(intx){key=x;left=null;right=null;}}classGfG{// Function to find the index of an element.staticintsearchValue(ArrayList<Integer>in,intvalue,ints,inte){for(inti=s;i<=e;i++){if(in.get(i)==value)returni;}return-1;}// Recursive function to build the binary tree.staticNodebuildTreeRecur(ArrayList<Integer>in,ArrayList<Integer>level,ints,inte){// For empty array, return nullif(s>e){returnnull;}// create the root NodeNoderoot=newNode(level.get(0));// find the index of first element of level array// in the in-order array.intindex=searchValue(in,level.get(0),s,e);intlCnt=index-s,rCnt=e-index;// Level order array for left and right subtree.ArrayList<Integer>lLevel=newArrayList<>(lCnt);ArrayList<Integer>rLevel=newArrayList<>(rCnt);// add the left and right elements to lLevel and rLevelintl=0,r=0;for(inti=1;i<e-s+1;i++){intj=searchValue(in,level.get(i),s,e);// If the value is present in left subtree.if(j<index)lLevel.add(level.get(i));// else it will be present in right subtree.elserLevel.add(level.get(i));}// Recursively create the left and right subtree.root.left=buildTreeRecur(in,lLevel,s,index-1);root.right=buildTreeRecur(in,rLevel,index+1,e);returnroot;}staticNodebuildTree(ArrayList<Integer>inorder,ArrayList<Integer>level,intn){// Build the tree recursively.returnbuildTreeRecur(inorder,level,0,n-1);}staticvoidprintInorder(Nodehead){if(head==null)return;printInorder(head.left);System.out.print(head.key+" ");printInorder(head.right);}publicstaticvoidmain(String[]args){ArrayList<Integer>in=newArrayList<>(Arrays.asList(4,8,10,12,14,20,22));ArrayList<Integer>level=newArrayList<>(Arrays.asList(20,8,22,4,12,10,14));intn=in.size();Noderoot=buildTree(in,level,n);printInorder(root);}}
Python
# Python Program to construct tree using # inorder and levelorder traversalsclassNode:def__init__(self,key):self.key=keyself.left=Noneself.right=None# Function to find the index of an element.defsearchValue(inorder,value,s,e):foriinrange(s,e+1):ifinorder[i]==value:returnireturn-1# Recursive function to build the binary tree.defbuildTreeRecur(inorder,level,s,e):# For empty array, return Noneifs>e:returnNone# create the root Noderoot=Node(level[0])# find the index of first element of level array# in the in-order array.index=searchValue(inorder,level[0],s,e)# Level order array for left and right subtree.lLevel=[]rLevel=[]# add the left and right elements to lLevel and rLevelforiinrange(1,e-s+1):j=searchValue(inorder,level[i],s,e)ifj<index:lLevel.append(level[i])else:rLevel.append(level[i])# Recursively create the left and right subtree.root.left=buildTreeRecur(inorder,lLevel,s,index-1)root.right=buildTreeRecur(inorder,rLevel,index+1,e)returnrootdefbuildTree(inorder,level,n):returnbuildTreeRecur(inorder,level,0,n-1)defprintInorder(head):ifheadisNone:returnprintInorder(head.left)print(head.key,end=" ")printInorder(head.right)if__name__=="__main__":inorder=[4,8,10,12,14,20,22]level=[20,8,22,4,12,10,14]n=len(inorder)root=buildTree(inorder,level,n)printInorder(root)
C#
// C# Program to construct tree using // inorder and levelorder traversalsusingSystem;usingSystem.Collections.Generic;classNode{publicintkey;publicNodeleft,right;publicNode(intx){key=x;left=null;right=null;}}classGfG{// Function to find the index of an element.staticintSearchValue(List<int>inorder,intvalue,ints,inte){for(inti=s;i<=e;i++){if(inorder[i]==value)returni;}return-1;}// Recursive function to build the binary tree.staticNodeBuildTreeRecur(List<int>inorder,List<int>level,ints,inte){// For empty array, return nullif(s>e){returnnull;}// create the root NodeNoderoot=newNode(level[0]);// find the index of first element of level array// in the in-order array.intindex=SearchValue(inorder,level[0],s,e);intlCnt=index-s,rCnt=e-index;// Level order array for left and right subtree.List<int>lLevel=newList<int>(lCnt);List<int>rLevel=newList<int>(rCnt);// add the left and right elements to lLevel and rLevelfor(inti=1;i<e-s+1;i++){intj=SearchValue(inorder,level[i],s,e);// If the value is present in left subtree.if(j<index)lLevel.Add(level[i]);elserLevel.Add(level[i]);}// Recursively create the left and right subtree.root.left=BuildTreeRecur(inorder,lLevel,s,index-1);root.right=BuildTreeRecur(inorder,rLevel,index+1,e);returnroot;}staticNodeBuildTree(List<int>inorder,List<int>level,intn){returnBuildTreeRecur(inorder,level,0,n-1);}staticvoidPrintInorder(Nodehead){if(head==null)return;PrintInorder(head.left);Console.Write(head.key+" ");PrintInorder(head.right);}staticvoidMain(string[]args){List<int>inorder=newList<int>{4,8,10,12,14,20,22};List<int>level=newList<int>{20,8,22,4,12,10,14};intn=inorder.Count;Noderoot=BuildTree(inorder,level,n);PrintInorder(root);}}
JavaScript
// JavaScript Program to construct tree using // inorder and levelorder traversalsclassNode{constructor(key){this.key=key;this.left=null;this.right=null;}}// Function to find the index of an element.functionsearchValue(inorder,value,s,e){for(leti=s;i<=e;i++){if(inorder[i]===value){returni;}}return-1;}// Recursive function to build the binary tree.functionbuildTreeRecur(inorder,level,s,e){// For empty array, return nullif(s>e){returnnull;}// create the root Nodeconstroot=newNode(level[0]);// find the index of first element of level array// in the in-order array.constindex=searchValue(inorder,level[0],s,e);// Level order array for left and right subtree.constlLevel=[];constrLevel=[];// add the left and right elements to lLevel and // rLevelletl=0,r=0;for(leti=1;i<e-s+1;i++){constj=searchValue(inorder,level[i],s,e);// If the value is present in// left subtree. if(j<index){lLevel[l++]=level[i];}// else it will be present in // right subtree.else{rLevel[r++]=level[i];}}// Recursively create the left and right subtree.root.left=buildTreeRecur(inorder,lLevel,s,index-1);root.right=buildTreeRecur(inorder,rLevel,index+1,e);returnroot;}functionbuildTree(inorder,level,n){returnbuildTreeRecur(inorder,level,0,n-1);}functionprintInorder(head){if(!head){return;}printInorder(head.left);console.log(head.key);printInorder(head.right);}constinorder=[4,8,10,12,14,20,22];constlevel=[20,8,22,4,12,10,14];constn=inorder.length;constroot=buildTree(inorder,level,n);printInorder(root);
Output
4 8 10 12 14 20 22
Time Complexity: O(n^3) Auxiliary Space: O(n), where n is the number of nodes.