标签:complete cst void search mic space name alt scan
题意:给定n个数构建完全二叉树,输出完全二叉树的层序遍历
思路:二叉树的中序遍历建树即为输出
#include<cstdio>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
const int N = 1010;
int num[N],CBT[N];
int n;
int index = 1;
void inOrder(int root){
if(root>n) return;
inOrder(root*2);
CBT[root]=num[index++];
inOrder(root*2+1);
}
int main(){
scanf("%d",&n);
vector<int> seq;
for(int i = 1;i<=n;i++){
int number;
scanf("%d",&number);
num[i] = number;
}
sort(num+1,num+n+1);//从小到大
inOrder(1);
for(int i = 1;i<=n;i++){
printf("%d",CBT[i]);
if(i!=n) printf(" ");
}
return 0;
}
PAT A1064 Complete Binary Search Tree (30分)(完全二叉树的性质以及二叉查找树的中序遍历有序)
标签:complete cst void search mic space name alt scan
原文地址:https://www.cnblogs.com/shuibeng/p/13657144.html