알고리즘/SW Expert Academy

[D3] 7985. Rooted Binary Tree 재구성

혬혬 2019. 10. 6. 11:05
728x90
#include <stdio.h>
int tree[3000];
int each_depth[12][1025];
int each_depth_index[12];
int max_depth = 0;
int squeared_2[11] = { 0 };
void seach_depth(int start, int end, int depth) {
	if (depth > max_depth)
		return;
	int box = (end-start) / 2;
	each_depth[depth][each_depth_index[depth]++] = tree[start + box];
	seach_depth(start, start + box - 1, depth + 1);
	seach_depth(start + box+1, end, depth + 1);
}
int main() {

	int test_case = 0;
	scanf("%d", &test_case);
	squeared_2[0] = 1;
	for (int i = 1; i <= 11; i++) {
		squeared_2[i] = squeared_2[i - 1] * 2;
	}
	for (int q = 0; q < test_case; q++) {
		scanf("%d", &max_depth);
		for (int i = 0; i < squeared_2[max_depth] - 1; i++) {
			scanf("%d", &tree[i]);
		}
		seach_depth(0, squeared_2[max_depth]-2, 1);
		printf("#%d ", q + 1);
		int tree_index = 0;
		for (int i = 1; i <= max_depth; i++) {
			for (int j = 0; j < each_depth_index[i]; j++) {
				printf("%d ", each_depth[i][j]);
				each_depth[i][j] = 0;
				tree[tree_index++] = 0;
			}
			each_depth_index[i] = 0;
			printf("\n");
		}
	}

	return 0;
}

 

728x90