#include<iostream.h>
#include<conio.h>
typedef struct tree
{
int key;
struct tree * left;
struct tree * right;
} Tree;
int search(int arr[],int left, int right,int key)
{
int i;
for(i=left; i <= right; i++)
{
if( key == arr[i])
{
return i;
}
}
return -1;
}
Tree * createTree(int postorder[],int inorder[],int left,int right, int pos)
{
if(left > right)
return NULL;
Tree *t = new tree;
t->key = postorder[pos];
int index = search(inorder,left, right, t->key);
cout<<"\n"<<t->key;
t->left = createTree(postorder,inorder ,left,index-1, pos - 1- (right - index));
t->right = createTree(postorder,inorder,index +1, right, pos -1);
return t;
}
void main(){
int postorder[] = {4,1,3,10,11,8,2,7};
int inorder[] = {4,10,3,1,7,11,8,2};
clrscr();
Tree *newTree = createTree(postorder,inorder,0,7,7);
getch();
}
#include<conio.h>
typedef struct tree
{
int key;
struct tree * left;
struct tree * right;
} Tree;
int search(int arr[],int left, int right,int key)
{
int i;
for(i=left; i <= right; i++)
{
if( key == arr[i])
{
return i;
}
}
return -1;
}
Tree * createTree(int postorder[],int inorder[],int left,int right, int pos)
{
if(left > right)
return NULL;
Tree *t = new tree;
t->key = postorder[pos];
int index = search(inorder,left, right, t->key);
cout<<"\n"<<t->key;
t->left = createTree(postorder,inorder ,left,index-1, pos - 1- (right - index));
t->right = createTree(postorder,inorder,index +1, right, pos -1);
return t;
}
void main(){
int postorder[] = {4,1,3,10,11,8,2,7};
int inorder[] = {4,10,3,1,7,11,8,2};
clrscr();
Tree *newTree = createTree(postorder,inorder,0,7,7);
getch();
}