代码如下:
#include <iostream>
using namespace std;
const int N = 1010;
int pre[N], in[N], post[N];struct node {int w;node *l, *r;node (int w = 0, node *l = NULL, node *r = NULL): w(w), l(l), r(r) {}
};void build(int l, int r, int &t, node *&root) {int flag = -1;for (int i = l; i <= r; i++) {if (in[i] == pre[t]) {flag = i;break;}}if (flag == -1)return ;root = new node(in[flag]);t++;if (flag > l)build(l, flag - 1, t, root->l);if (flag < r)build(flag + 1, r, t, root->r);
}void post_order(node *root) {if (root != NULL) {post_order(root->l);post_order(root->r);cout << root->w << " ";}
}int main() {int n;cin >> n;for (int i = 1; i <= n; i++)cin >> pre[i];for (int i = 1; i <= n; i++)cin >> in[i];int t = 1;node *root;build(1, n, t, root);post_order(root);return 0;
}