PAT A1086

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值