#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
vector<int> op;
struct node
{
node* lchild;
node* rchild;
int data;
};
int post[40],in[40];
int postL,postR,inL,inR;
node* create(int postL,int postR,int inL,int inR)
{
if(postL>postR)
return NULL;
node* root=new node;
root->data=post[postR];
int i;
for(i=inL;i<=inR;i++)
{
if(in[i]==post[postR])
break;
}
int numleft=i-inL;
root->lchild=create(postL,postL+numleft-1,inL,i-1);
root->rchild=create(postL+numleft,postR-1,i+1,inR);
return root;
}
void level_traverse(node* root)
{
queue<node*> q;
q.push(root);
while(!q.empty())
{
node* top=q.front();
q.pop();
op.push_back(top->data);
if(top->lchild!=NULL)
q.push(top->lchild);
if(top->rchild!=NULL)
q.push(top->rchild);
}
}
int main()
{
int n;
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=create(0,n-1,0,n-1);
level_traverse(root);
for(int i=0;i<(op.size()-1);i++)
{
printf("%d ",op[i]);
}
printf("%d",op[op.size()-1]);
system("pause");
return 0;
}
PAT 1020
最新推荐文章于 2021-04-11 17:55:34 发布