solution
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 31;
int in[N], post[N];
struct node
{
int idx, value;
};
bool cmp(node x, node y)
{
return x.idx < y.idx;
}
vector<node> ans;
void pre(int root, int start, int end, int idx)
{
if (start > end)
return;
int i = start;
while (i < end && in[i] != post[root])
i++;
ans.push_back({idx, post[root]});
pre(root - (end - i + 1), start, i - 1, idx * 2 + 1);
pre(root - 1, i + 1, end, idx * 2 + 2);
}
int main()
{
int n;
cin>>n;
for (int i = 0; i < n; i++)
{
cin >> post[i];
}
for (int i = 0; i < n; i++)
{
cin >> in[i];
}
pre(n - 1, 0, n - 1, 0);
sort(ans.begin(), ans.end(), cmp);
for (int i = 0; i < ans.size(); i++)
{
if(i)cout<<" ";
cout << ans[i].value;
}
}