#include<bits/stdc++.h>
using namespace std;
const int maxn=37;
int zx[maxn];
int hx[maxn];
int n;
struct node{
int l;
int r;
};
node pp[maxn];
//la,ra表示后序
int build(int la,int ra,int lb,int rb)
{
if(lb>rb)
return 0;
int root=hx[ra];
int pos1;
int pos2=lb;
//pos1表示左子树的个数,pos2表示根在中序的位置
while(zx[pos2]!=root)
pos2++;
pos1=pos2-lb;
pp[root].l=build(la,la+pos1-1,lb,pos2-1);
pp[root].r=build(la+pos1,ra-1,pos2+1,rb);
return root;
}
void bfs()
{
queue<int> q;
q.push(hx[n]);
printf("%d",hx[n]);
while(!q.empty())
{
// cout<<1<<endl;
int now=q.front();
q.pop();
if(now!=hx[n])
printf(" %d",now);
if(pp[now].l)
q.push(pp[now].l);
if(pp[now].r)
q.push(pp[now].r);
}
printf("\n");
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>hx[i];
}
for(int i=1;i<=n;i++)
{
cin>>zx[i];
}
build(1,n,1,n);
bfs();
return 0;
}
06-29
675
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
11-24