#include<iostream>
using namespace std;
struct BTreeNode
{
int data;
struct BTreeNode *lchild,*rchild;
};
void PostOrder(BTreeNode *bt)
{
if(bt!=NULL)
{
PostOrder(bt->lchild);
PostOrder(bt->rchild);
printf("%d\n",bt->data);
}
}
void PreInCreate(BTreeNode *&root,int pre[100],int in[100],int start1,int end1,int start2,int end2)
{
root=(BTreeNode*)malloc(sizeof(BTreeNode*));
root->data=pre[start1];
//输入序列无效
root->lchild=root->rchild=NULL;
if(start1==end1)
{
if(start2==end2&&pre[start1]==in[start2])
return;
else
{
throw std::exception("Invalid input.");
}
}
int i;
for(i=start2;i<=end2;i++)
{
if(in[i]==pre[start1])break;
}
if(i==start2)
{
root->lchild=NULL;
}
else
{
PreInCreate(root->lchild,pre,in,start1+1,start1+(i-start2),start2,i-1);
}
if(i==end2)
{
root->rchild=NULL;
}
else
{
PreInCreate(root->rchild,pre,in,start1+(i-start2)+1,end1,i+1,end2);
}
}
int main()
{
int n;
while(cin>>n)
{
int pre[100],in[100];
for(int i=0;i<n;i++)
{
cin>>pre[i];
}
for(int i=0;i<n;i++)
{
cin>>in[i];
}
BTreeNode *bt=(BTreeNode*)malloc(sizeof(BTreeNode*));
PreInCreate(bt,pre,in,0,n-1,0,n-1);
PostOrder(bt);
}
return 0;
}