//树的问题核心是以不同的形式给出,让你构造出唯一的一棵(二叉)树,然后再遍历输出
#include<iostream>
#include<string>
using namespace std;
int n, cnt = 0;
bool first = true;
class Node{
public:
int val;
Node* left;
Node* right;
Node(int _val){
this->val = _val;
this->left = NULL;
this->right = NULL;
}
};
Node* build_tree(){//相当于按照先根的次序建树,pop代表该结点为空
Node* node = NULL;
if (cnt < 2 * n){
string s;
cin >> s;
++cnt;
if (s == "Push"){
int num;
cin >> num;
node = new Node(num);
node->left = build_tree();//值得玩味啊~~~
node->right = build_tree();//压栈的规律,先左后右,然后上溯。
}
}
return node;
}
void postorder(Node* node){
if (node == NULL)return;
else{
postorder(node->left);
postorder(node->right);
if (first){
cout << node->val;
first = false;
}
else
{
cout << " " << node->val;
}
}
}
int main(){
cin >> n;
Node* root = build_tree();
postorder(root);
}