Given Inorder Traversal of a Special Binary Tree in which the key of every node is greater than keys in left and right children, construct the Binary Tree and return root.
Examples:
Input: inorder[] = {5, 10, 40, 30, 28}
Output: root of following tree
40
/ \
10 30
/ \
5 28
Input: inorder[] = {1, 5, 10, 40, 30,
15, 28, 20}
Output: root of following tree
40
/ \
10 30
/ \
5 28
/ / \
1 15 20
The idea used in Construction of Tree from given Inorder and Preorder traversals can be used here. Let the given array is {1, 5, 10, 40, 30, 15, 28, 20}. The maximum element in the given array must be root. The elements on the left side of the maximum element are in the left subtree and the elements on the right side are in the right subtree.
40
/ \
{1,5,10} {30,15,28,20}
We recursively follow the above step for left and right subtrees, and finally, get the following tree.
40
/ \
10 30
/ \
5 28
/ / \
1 15 20
Algorithm: buildTree()
- Find index of the maximum element in array. The maximum element must be root of Binary Tree.
- Create a new tree node ‘root’ with the data as the maximum value found in step 1.
- Call buildTree for elements before the maximum element and make the built tree as left subtree of ‘root’.
- Call buildTree for elements after the maximum element and make the built tree as right subtree of ‘root’.
- return ‘root’.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
class node
{
public :
int data;
node* left;
node* right;
};
int max( int inorder[], int strt, int end);
node* newNode( int data);
node* buildTree ( int inorder[], int start, int end)
{
if (start > end)
return NULL;
int i = max (inorder, start, end);
node *root = newNode(inorder[i]);
if (start == end)
return root;
root->left = buildTree (inorder, start, i - 1);
root->right = buildTree (inorder, i + 1, end);
return root;
}
int max ( int arr[], int strt, int end)
{
int i, max = arr[strt], maxind = strt;
for (i = strt + 1; i <= end; i++)
{
if (arr[i] > max)
{
max = arr[i];
maxind = i;
}
}
return maxind;
}
node* newNode ( int data)
{
node* Node = new node();
Node->data = data;
Node->left = NULL;
Node->right = NULL;
return Node;
}
void printInorder (node* node)
{
if (node == NULL)
return ;
printInorder (node->left);
cout<<node->data<< " " ;
printInorder (node->right);
}
int main()
{
int inorder[] = {5, 10, 40, 30, 28};
int len = sizeof (inorder)/ sizeof (inorder[0]);
node *root = buildTree(inorder, 0, len - 1);
cout << "Inorder traversal of the constructed tree is \n" ;
printInorder(root);
return 0;
}
|
C
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* left;
struct node* right;
};
int max( int inorder[], int strt, int end);
struct node* newNode( int data);
struct node* buildTree ( int inorder[], int start, int end)
{
if (start > end)
return NULL;
int i = max (inorder, start, end);
struct node *root = newNode(inorder[i]);
if (start == end)
return root;
root->left = buildTree (inorder, start, i-1);
root->right = buildTree (inorder, i+1, end);
return root;
}
int max ( int arr[], int strt, int end)
{
int i, max = arr[strt], maxind = strt;
for (i = strt+1; i <= end; i++)
{
if (arr[i] > max)
{
max = arr[i];
maxind = i;
}
}
return maxind;
}
struct node* newNode ( int data)
{
struct node* node = ( struct node*) malloc ( sizeof ( struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
void printInorder ( struct node* node)
{
if (node == NULL)
return ;
printInorder (node->left);
printf ( "%d " , node->data);
printInorder (node->right);
}
int main()
{
int inorder[] = {5, 10, 40, 30, 28};
int len = sizeof (inorder)/ sizeof (inorder[0]);
struct node *root = buildTree(inorder, 0, len - 1);
printf ( "\n Inorder traversal of the constructed tree is \n" );
printInorder(root);
return 0;
}
|
Java
class Node
{
int data;
Node left, right;
Node( int item)
{
data = item;
left = right = null ;
}
}
class BinaryTree
{
Node root;
Node buildTree( int inorder[], int start, int end, Node node)
{
if (start > end)
return null ;
int i = max(inorder, start, end);
node = new Node(inorder[i]);
if (start == end)
return node;
node.left = buildTree(inorder, start, i - 1 , node.left);
node.right = buildTree(inorder, i + 1 , end, node.right);
return node;
}
int max( int arr[], int strt, int end)
{
int i, max = arr[strt], maxind = strt;
for (i = strt + 1 ; i <= end; i++)
{
if (arr[i] > max)
{
max = arr[i];
maxind = i;
}
}
return maxind;
}
void printInorder(Node node)
{
if (node == null )
return ;
printInorder(node.left);
System.out.print(node.data + " " );
printInorder(node.right);
}
public static void main(String args[])
{
BinaryTree tree = new BinaryTree();
int inorder[] = new int []{ 5 , 10 , 40 , 30 , 28 };
int len = inorder.length;
Node mynode = tree.buildTree(inorder, 0 , len - 1 , tree.root);
System.out.println( "Inorder traversal of the constructed tree is " );
tree.printInorder(mynode);
}
}
|
Python3
class newNode:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def buildTree (inorder, start, end):
if start > end:
return None
i = Max (inorder, start, end)
root = newNode(inorder[i])
if start = = end:
return root
root.left = buildTree (inorder, start, i - 1 )
root.right = buildTree (inorder, i + 1 , end)
return root
def Max (arr, strt, end):
i, Max = 0 , arr[strt]
maxind = strt
for i in range (strt + 1 , end + 1 ):
if arr[i] > Max :
Max = arr[i]
maxind = i
return maxind
def printInorder (node):
if node = = None :
return
printInorder (node.left)
print (node.data, end = " " )
printInorder (node.right)
if __name__ = = '__main__' :
inorder = [ 5 , 10 , 40 , 30 , 28 ]
Len = len (inorder)
root = buildTree(inorder, 0 , Len - 1 )
print ( "Inorder traversal of the" ,
"constructed tree is " )
printInorder(root)
|
C#
using System;
public class Node
{
public int data;
public Node left, right;
public Node( int item)
{
data = item;
left = right = null ;
}
}
class GFG
{
public Node root;
public virtual Node buildTree( int [] inorder, int start,
int end, Node node)
{
if (start > end)
{
return null ;
}
int i = max(inorder, start, end);
node = new Node(inorder[i]);
if (start == end)
{
return node;
}
node.left = buildTree(inorder, start,
i - 1, node.left);
node.right = buildTree(inorder, i + 1,
end, node.right);
return node;
}
public virtual int max( int [] arr,
int strt, int end)
{
int i, max = arr[strt], maxind = strt;
for (i = strt + 1; i <= end; i++)
{
if (arr[i] > max)
{
max = arr[i];
maxind = i;
}
}
return maxind;
}
public virtual void printInorder(Node node)
{
if (node == null )
{
return ;
}
printInorder(node.left);
Console.Write(node.data + " " );
printInorder(node.right);
}
public static void Main( string [] args)
{
GFG tree = new GFG();
int [] inorder = new int []{5, 10, 40, 30, 28};
int len = inorder.Length;
Node mynode = tree.buildTree(inorder, 0,
len - 1, tree.root);
Console.WriteLine( "Inorder traversal of " +
"the constructed tree is " );
tree.printInorder(mynode);
}
}
|
Javascript
<script>
class Node
{
constructor(item)
{
this .data = item;
this .left = null ;
this .right = null ;
}
}
var root = null ;
function buildTree(inorder, start, end, node)
{
if (start > end)
{
return null ;
}
var i = max(inorder, start, end);
node = new Node(inorder[i]);
if (start == end)
{
return node;
}
node.left = buildTree(inorder, start,
i - 1, node.left);
node.right = buildTree(inorder, i + 1,
end, node.right);
return node;
}
function max(arr, strt, end)
{
var i, maxi = arr[strt], maxind = strt;
for (i = strt + 1; i <= end; i++)
{
if (arr[i] > maxi)
{
maxi = arr[i];
maxind = i;
}
}
return maxind;
}
function printInorder(node)
{
if (node == null )
{
return ;
}
printInorder(node.left);
document.write(node.data + " " );
printInorder(node.right);
}
var inorder = [5, 10, 40, 30, 28];
var len = inorder.length;
var mynode = buildTree(inorder, 0, len - 1, root);
document.write( "Inorder traversal of " +
"the constructed tree is <br>" );
printInorder(mynode);
</script>
|
Output
Inorder traversal of the constructed tree is
5 10 40 30 28
Time Complexity: O(n^2)
Auxiliary Space: O(n), The extra space used is due to the recursion call stack.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
01 Aug, 2022
Like Article
Save Article