Skip to content

Instantly share code, notes, and snippets.

@surinoel
Created July 5, 2019 11:35
Show Gist options
  • Select an option

  • Save surinoel/f64db9b6c83a5f88c8aa41d109baad0b to your computer and use it in GitHub Desktop.

Select an option

Save surinoel/f64db9b6c83a5f88c8aa41d109baad0b to your computer and use it in GitHub Desktop.
#include <vector>
#include <iostream>
using namespace std;
vector<int> inod, postod;
int inod_root[100001];
void find_preod(int a, int b, int c, int d) {
if (b - a == 0) return;
int root = postod[d - 1];
int range = inod_root[root];
cout << root << ' ';
find_preod(a, range, c, c + range - a);
find_preod(range + 1, b, c + range - a, d - 1);
return;
}
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
inod.resize(n), postod.resize(n);
for (int i = 0; i < n; i++) {
cin >> inod[i];
inod_root[inod[i]] = i;
}
for (int i = 0; i < n; i++) {
cin >> postod[i];
}
find_preod(0, n, 0, n);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment