int preTree[N_MAX]; int inTree[N_MAX]; int postTree[N_MAX]; void guessPostTree(int preS, int preE, int inS, int inE, int postS, int postE) { if (postS > postE) return; int root; for (root = inS; root <= inE; root++){ if (preTree[preS] == inTree[root]) break; } postTree[postE] = inTree[root]; int leftLen = root - inS; guessPostTree(preS + 1, preS + leftLen, inS, root - 1, postS, postS + leftLen - 1); guessPostTree(preS + leftLen + 1, preE, root + 1, inE, postS + leftLen, postE - 1); cout << inTree[root] << " "; } int main() { int T, N, i, j; cin >> T; for (int tc = 0; tc < T; tc++){ cin >> N; for (i = 0; i < N; i++) cin >> preTree[i]; for (i = 0; i < N; i++) cin >> inTree[i]; vector<int> v1(preTree, preTree + N); vector<int> v2(inTree, inTree + N); guessPostTree(0, N - 1, 0, N - 1, 0, N - 1); cout << endl; } return 0; }
|