#include <iostream>
#include <vector>
#include <stack>
#include <cstring>
using namespace std;
//push 中序
//pop 前序
vector<int> pre, in, post, value;
void func(int root, int start, int end)
{
if (start > end)
{
return;
}
int i = start;
while (i < end && in[i] != pre[root])
{
i++;
}
func(root + 1, start, i - 1);
func(root + 1 + i - start, i + 1, end);
post.push_back(pre[root]);
}
int main()
{
int n;
scanf("%d", &n);
string str;
stack<int> s;
int t = 2 * n;
while (t--)
{
cin >> str;
if (str == "Push")
{
int num;
scanf("%d", &num);
pre.push_back(num);
s.push(num);
}
else
{
in.push_back(s.top());
s.pop();
}
}
func(0, 0, n - 1);
printf("%d", post[0]);
for (int i = 1; i < n; i++)
{
printf(" %d", post[i]);
}
return 0;
}
PAT A1086
最新推荐文章于 2021-08-18 22:33:27 发布