A1064

没写出.
本题是由已知完全二叉树遍历得到中序遍历序列的逆用.
已知完全二叉树中序遍历序列以及树的轮廓,有了树的轮廓框架,即可进行中序遍历,从而将中序遍历序列中的值依次填充到树的框架中,充实树,最后得到完整的树(拥有权重即每个结点的值).
用数组存储,遍历过程与链表一致,由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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值