没写出.
本题是由已知完全二叉树遍历得到中序遍历序列的逆用.
已知完全二叉树中序遍历序列以及树的轮廓,有了树的轮廓框架,即可进行中序遍历,从而将中序遍历序列中的值依次填充到树的框架中,充实树,最后得到完整的树(拥有权重即每个结点的值).
用数组存储,遍历过程与链表一致,由root2代替root->lchild,root2+1代替root->rchild.
同类型题:A1099
#include<cstdio>
#include<cstdlib>
#include<string.h>
#include<math.h>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=1010;
int a[maxn],res[maxn],idx=0;
void order(int root,int n){ //完全二叉树中序遍历的逆用.
if(root>n){
return;
}
order(root*2,n);
res[root]=a[idx++];
order(root*2+1,n);
}
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("1.txt","r",stdin);
#endif
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
sort(a,a+n);
order(1,n);
for(int i=1;i<=n;i++){
printf("%d",res[i]);
if(i!=n){
printf(" ");
}
}
return 0;
}