#include <string.h>
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#define N 1002
using namespace std;
struct tree
{
int data;
struct tree *right, *left;
};
int ok;
struct tree *create( int len, int *Pr, int *In )
{
int i;
struct tree *root;
root = (struct tree *)malloc(sizeof(struct tree));
if ( len == 0 )
{
return root = NULL;
}
root->data = Pr[0];
for ( i = 0;i < len ; i++ )
{
if ( Pr[0] == In[i] )
break;
}
root->left = create ( i, Pr+1, In );
root->right = create ( len-i-1, Pr+i+1, In+i+1 );
return root;
}
void AFTER_order ( struct tree *root )
{
if ( root )
{
AFTER_order ( root->left );
AFTER_order ( root->right );
if ( ok == 0 )
{
printf ( "%d", root->data );
ok = 1;
}
else
printf(" %d", root->data );
}
}
int main()
{
int t, i;
int Pr[N], In[N];
while ( ~scanf ( "%d", &t ) )
{
for( i = 0;i < t ; i++ )
{
scanf ( "%d", &Pr[i] );
}
for( i = 0;i < t ; i++ )
{
scanf ( "%d", &In[i] );
}
struct tree *root;
root = create ( t, Pr, In );
ok = 0;
AFTER_order ( root );
printf ( "\n" );
}
return 0;
}
代码菜鸟,如有错误,请多包涵!!!