04-树6 Complete Binary Search Tree (30 分)

版权声明:本文为博主原创文章,需表明来源,方可随意转载。 https://blog.csdn.net/qq_36589706/article/details/83420086

#include<cstdio>
#include<stdlib.h>
const int maxn=1001;
int arr[maxn], brr[maxn];
int pos=0, n;
int cmp(const void *a, const void *b){
	return(*(int *)a-*(int *)b);
}
void solve(int root){//利用完全二叉树的特性
	if(root<=n){
		solve(root*2);
		brr[root]=arr[pos++];
		solve(root*2+1);
	}
}
int main(){
	scanf("%d", &n);
	 for(int i=0; i<n; i++){
	 	scanf("%d", &arr[i]);
	 }
	 qsort(arr, n, sizeof(int), cmp);//快排
	 solve(1);
	 printf("%d", brr[1]);
	 for(int i=2; i<=n; i++){
	 	printf(" %d", brr[i]);
	 }
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36589706/article/details/83420086
今日推荐