Skip to content

Instantly share code, notes, and snippets.

@surinoel
Last active July 5, 2019 11:25
Show Gist options
  • Save surinoel/4cb3784796a3ac1fb815d1a1017edae6 to your computer and use it in GitHub Desktop.
Save surinoel/4cb3784796a3ac1fb815d1a1017edae6 to your computer and use it in GitHub Desktop.
#include <vector>
#include <iostream>
using namespace std;
vector<int> inod, postod;
void find_preod(int a, int b, int c, int d) {
if (b - a == 0) return;
int root = postod[d - 1];
int range = a;
for (int i = a; i < b; i++) {
if (inod[i] == root) {
range = i;
}
}
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];
}
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