前序+中序求后序
#include <bits/stdc++.h>
using namespace std;
const int N = 40;
int n;
vector<int> pre, in, post;
void build(int il, int ir, int pl, int pr){
int root = pre[pl];
int k = il;
while(k <= ir && in[k] != root) k++;
if(il < k) build(il, k - 1, pl + 1, pr - ir + k);
if(ir > k) build(k + 1, ir, pr - ir + k + 1, pr);
post.push_back(root);
}
int main(){
cin >> n;
stack<int> st;
for(int i = 0; i < 2 * n; i++){
string ope;
cin >> ope;
if(ope == "Push"){
int x;
cin >> x;
pre.push_back(x);
st.push(x);
}
else{
int x = st.top();
in.push_back(x);
st.pop();
}
}
build(0, n - 1, 0, n - 1);
for(auto&v : post) cout << v << ' ';
}