#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
struct node
{
long left,right;
}tree[];
long a[],b[];
void work(long l,long r,long p,long q,long fa,long cond)
{
long root=a[r],pos;
for (pos=p;pos<=q;pos++)
if (b[pos]==root)
break;
if (cond==)
tree[fa].left=root;
else
tree[fa].right=root;
if (p<=pos-)
work(l,l+pos--p,p,pos-,root,);
if (pos+<=q)
work(l+pos-p,r-,pos+,q,root,);
}
int main()
{
long i,head,tail,n;
long q[];
scanf("%ld",&n);
for (i=;i<=n;i++)
{
scanf("%ld",&a[i]);
tree[a[i]].left=;
tree[a[i]].right=;
}
for (i=;i<=n;i++)
scanf("%ld",&b[i]);
work(,n,,n,,);
head=;
tail=;
q[]=a[n];
while (head<tail)
{
head++;
printf("%ld",q[head]);
if (tree[q[head]].left!=)
{
tail++;
q[tail]=tree[q[head]].left;
}
if (tree[q[head]].right!=)
{
tail++;
q[tail]=tree[q[head]].right;
}
if (head!=tail)
printf(" ");
}
return ;
}
#include <stdio.h>
#include <stdlib.h>
struct node
{
long left,right;
}tree[];
long a[],b[],q[];
void work(long x,long y,long p,long q,long father,long cond)
{
long pos,count;
if (father!=)
{
if (cond==)
tree[father].left=b[p];
else
tree[father].right=b[p];
}
for (pos=x;pos<=y;pos++)
if (a[pos]==b[p])
break;
count=pos-x;
if (count>)
work(x,pos-,p+,p+count,b[p],);
if (y-pos>)
work(pos+,y,p+count+,q,b[p],);
}
void change(long d)
{
if (tree[d].left== && tree[d].right==)
return ;
long temp;
temp=tree[d].left;
tree[d].left=tree[d].right;
tree[d].right=temp;
change(tree[d].left);
change(tree[d].right);
}
int main()
{
long n,i,head,tail;
scanf("%ld",&n);
for (i=;i<=n;i++)
scanf("%ld",&a[i]); //mid
for (i=;i<=n;i++)
scanf("%ld",&b[i]); //pre
for (i=;i<=n;i++)
{
tree[i].left=;
tree[i].right=;
}
work(,n,,n,,);
change(b[]);
head=; tail=;
q[]=b[];
while (head<tail)
{
head++;
printf("%ld",q[head]);
if (tree[q[head]].left!=)
{
tail++;
q[tail]=tree[q[head]].left;
}
if (tree[q[head]].right!=)
{
tail++;
q[tail]=tree[q[head]].right;
}
if (head!=tail)
printf(" "); //output不能多余的空格
}
return ;
}