BINTREE - Duyệt cây nhị phân

Tác giả: happyboy99x

Ngôn ngữ: C++

#include<cstdio>

const int N = 50000 + 5;
int n, pre[N], in[N], pIn[N];

void construct(int a, int b, int n) {
	if(n == 0) return;
	int pos = pIn[pre[a]];
	construct(a + 1, b, pos - b);
	construct(a + 1 + pos - b, pos + 1, b + n - pos - 1);
	printf("%d ", pre[a]);
}

void enter() {
	scanf("%d", &n);
	for(int i = 0; i < n; ++i)
		scanf("%d", &pre[i]);
	for(int i = 0; i < n; ++i) {
		scanf("%d", &in[i]);
		pIn[in[i]] = i;
	}
}

int main() {
	enter();
	construct(0, 0, n);
	return 0;
}

Download