#define M 100
struct BTN
{
int value;
struct BTN* left;
struct BTN *right;
};
int pre[8]={1,2,4,7,3,5,6,8};
int mid[8]={4,7,2,1,5,3,8,6};
int n=8;

struct BTN *rebuild(int k,int l,int r)
{
if(k>=n||l>r)
return NULL;
struct BTN *root=new struct BTN;
root->value=pre[k];
root->left=root->right=NULL;
for(int i=l;i<=r;i++)
{
if(mid[i]==pre[k])
{
root->left=rebuild(k+1,l,i-1);
root->right=rebuild(k+1+i-l,i+1,r);
return root;
}
}
}

struct BTN *rebuild_after(int n)
{

return rebuild(0,0,n-1);
}

void after_print(struct BTN *root)
{
if(root==NULL)
return ;
after_print(root->left);
after_print(root->right);
printf("%d ",root->value);
}


int main()
{
string s;
n=8;
struct BTN *root=rebuild_after(n);
after_print(root);
}