ALDS1_7_D Tree - Reconstruction of a Tree
# 目次
# 問題
# 解答
int n, pos;
vector<int> in, pre, post;
void rec(int l, int r) {
if (l >= r) return;
int root = pre[pos++];
int m = distance(in.begin(), find(in.begin(), in.end(), root));
rec(l, m);
rec(m + 1, r);
post.push_back(root);
}
void solve() {
pos = 0;
rec(0, pre.size());
int i;
loop(i, 0, n) {
if (i) cout << " ";
cout << post[i];
}
cout << endl;
}
int main(void) {
int x;
cin >> n;
loop(i, 0, n) {
cin >> x;
pre.push_back(x);
}
loop(i, 0, n) {
cin >> x;
in.push_back(x);
}
solve();
}