题意:
输入一个正整数N(<=1000),接着输入N个非负整数(<=2000),输出完全二叉树的层次遍历。
AAAAAccepted code:
1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 int a[1007]; 5 int tree[1007]; 6 int cnt; 7 int n; 8 void dfs(int x){ 9 if(x>n) 10 return; 11 dfs(x*2); 12 tree[x]=a[++cnt]; 13 dfs(x*2+1); 14 } 15 int main(){ 16 ios::sync_with_stdio(false); 17 cin.tie(NULL); 18 cout.tie(NULL); 19 cin>>n; 20 for(int i=1;i<=n;++i) 21 cin>>a[i]; 22 sort(a+1,a+1+n); 23 dfs(1); 24 cout<<tree[1]; 25 for(int i=2;i<=n;++i) 26 cout<<" "<<tree[i]; 27 return 0; 28 }