#include <iostream>
#include <string>
using namespace std;
char str[10];
int number,index=0,N;
struct Node
{
int val;
Node *left;
Node *right;
Node(int v)
{
val=v;
left=NULL;
right=NULL;
}
};
int idx=0;
Node* buildTree()
{
Node *r=NULL;
if(idx<2*N)
{
scanf("%s",&str);
if(str[1]=='u')
{
scanf("%d",&number);
r=new Node(number);
idx++;
}
else if(str[1]=='o')
{
idx++;
return NULL;
}
r->left=buildTree();
r->right=buildTree();
}
return r;
}
int outIdx=0;
void postorder(Node *r)
{
if(r!=NULL)
{
postorder(r->left);
postorder(r->right);
if(outIdx==0)
{
printf("%d",r->val);
outIdx++;
}
else
printf(" %d",r->val);
}
}
int main()
{
scanf("%d",&N);
Node *root=buildTree();
postorder(root);
return 0;
}
本题考查树的建立和后根遍历。