constint N = 40; int n, postOrder[N], inOrder[N]; unordered_map<int, int> l, r, pos;
// 中序左右,后序左右 intbuild(int il, int ir, int pl, int pr){ int root = postOrder[pr]; int k = pos[root]; if(il < k) l[root] = build(il, k - 1, pl, k - 1 - il + pl); if(ir > k) r[root] = build(k + 1, ir, k - 1 - il + pl + 1, pr - 1); return root; }
voidbfs(int root){ queue<int> q; q.push(root); while(q.size()){ int k = q.front(); q.pop(); cout << k << " "; if(l.count(k)) q.push(l[k]); if(r.count(k)) q.push(r[k]); } }
intmain(){ cin >> n;
for(int i = 0; i < n; i ++) cin >> postOrder[i]; for(int i = 0; i < n; i ++) cin >> inOrder[i], pos[inOrder[i]] = i;