#include<iostream>
using namespace std;
class inpost{
private:
typedef struct tree
{
int key;
struct tree * left;
struct tree * right;
} Tree;
public:
int search(int [],int , int ,int );
void createTree(int [],int [],int ,int , int );
};
int inpost::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;
}
void inpost::createTree(int postorder[],int inorder[],int left,int right, int pos)
{
if(left > right)
return;
else{
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);
}
}
int main(){
inpost ipp;
int postorder[] = {4,1,3,10,11,8,2,7};
int inorder[] = {4,10,3,1,7,11,8,2};
ipp.createTree(postorder,inorder,0,7,7);
}
using namespace std;
class inpost{
private:
typedef struct tree
{
int key;
struct tree * left;
struct tree * right;
} Tree;
public:
int search(int [],int , int ,int );
void createTree(int [],int [],int ,int , int );
};
int inpost::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;
}
void inpost::createTree(int postorder[],int inorder[],int left,int right, int pos)
{
if(left > right)
return;
else{
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);
}
}
int main(){
inpost ipp;
int postorder[] = {4,1,3,10,11,8,2,7};
int inorder[] = {4,10,3,1,7,11,8,2};
ipp.createTree(postorder,inorder,0,7,7);
}