版权声明:// Copyright © 2018年 Coding18. All rights reserved. https://blog.csdn.net/Coding18/article/details/86011889
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 40;
int n;
int post[maxn],in[maxn];
struct Node{
struct Node * lchild,*rchild;
int data;
};
Node * create(int pl,int pr,int il,int ir)
{
if(pr < pl) return NULL;
Node * root = new Node;
root->data = post[pr];
int k;
for(k = il; k <= ir; k++)
{
if(in[k] == post[pr])
break;
}
root->lchild = create(pl,pl+k-il-1,il,k-1);
root->rchild = create(pl+k-il,pr-1,k+1,ir);
return root;
}
void bfs(Node * root)
{
queue <Node*> q;
q.push(root);
while(!q.empty())
{
Node * temp = q.front();
q.pop();
if(temp != root) printf(" ");
printf("%d",temp->data);
if(temp->lchild != NULL) q.push(temp->lchild);
if(temp->rchild != NULL) q.push(temp->rchild);
}
}
int main()
{
scanf("%d",&n);
for(int i = 0; i < n; i++){
scanf("%d",&post[i]);
}
for(int i = 0; i < n ; i++){
scanf("%d",&in[i]);
}
Node * root = NULL;
root = create(0,n-1,0,n-1);
bfs(root);
return 0;
}