#include <iostream>
#include <stack>
using namespace std;
stack<int> s;
int in[50],innum=0,pre[50],prenum=0,N;
struct node{
int data;
node *lchild,*rchild;
};
node* createtree(int preL,int preR,int inL,int inR){
if(preL>preR) return NULL;
node *root=new node;
root->data=pre[preL];
int i=inL;
for(;i<=inR;i++)
if(in[i]==pre[preL]) break;
root->lchild=createtree(preL+1,preL+(i-inL),inL,i-1);
root->rchild=createtree(preL+1+(i-inL),preR,i+1,inR);
return root;
}
int countn=0;
void postorder(node* root){
if(root==NULL) return;
postorder(root->lchild);
postorder(root->rchild);
printf("%d",root->data);
if(++countn!=N) printf(" ");
}
int main(){
int temp;
char str[6];
scanf("%d",&N);
for(int i=0;i<N*2;i++){
scanf("%s",str);
if(str[1]=='u'){
scanf("%d",&temp);
s.push(temp);
pre[prenum++]=temp;
}else{
in[innum++]=s.top();
s.pop();
}
}
node *root=createtree(0,N-1,0,N-1);
postorder(root);
return 0;
}
PAT A1086
最新推荐文章于 2021-10-26 21:06:45 发布