当前位置:网站首页>完全二叉搜索树 (30 分)
完全二叉搜索树 (30 分)
2022-04-23 08:35:00 【怀化第二深情】
一个无重复的非负整数序列,必定对应唯一的一棵形状为完全二叉树的二叉搜索树。本题就要求你输出这棵树的层序遍历序列。
输入格式:
首先第一行给出一个正整数 N(≤1000),随后第二行给出 N 个不重复的非负整数。数字间以空格分隔,所有数字不超过 2000。
输出格式:
在一行中输出这棵树的层序遍历序列。数字间以 1 个空格分隔,行首尾不得有多余空格。
输入样例:
10
1 2 3 4 5 6 7 8 9 0
输出样例:
6 3 8 1 5 7 9 0 2 4
#include<bits/stdc++.h>
using namespace std;
int g[1010],in[1010];
int id,n;
void pre(int root){
if(root>n)return;
pre(root*2);
in[root]=g[id++];
pre(root*2+1);
}
int main(){
cin>>n;
for(int i=0;i<n;i++)cin>>g[i];
sort(g,g+n);
pre(1);
cout<<in[1];
for(int i=2;i<=n;i++)cout<<" "<<in[i];
}
版权声明
本文为[怀化第二深情]所创,转载请带上原文链接,感谢
https://blog.csdn.net/dege2929512534/article/details/124339415
边栏推荐
- input元素添加监听事件
- Go语言自学系列 | golang方法
- Stm32f103zet6 [development of standard library functions] - Introduction to library functions
- 关于数组复制问题
- 【路科V0】验证环境2——验证环境组件
- One click cleanup of pycharm and jupyter cache files under the project
- synchronized 锁的基本用法
- 一个必看的微信小程序开发指南1-基础知识了解
- 【58】最后一个单词的长度【LeetCode】
- Shell script advanced
猜你喜欢
随机推荐
rembg 分割mask
正点原子携手OneOS直播 OneOS系统教程全面上线
RCC introduction of Hal Library
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
ATSS(CVPR2020)
怎样读取Excel表格到数据库
Let the earth have less "carbon" and rest on the road
Reference passing 1
[C语言] 文件操作《一》
Excle plus watermark
Add listening event to input element
队列(c语言/链表)
Large amount of data submitted by form post
根据字节码获取类的绝对路径
Go语言自学系列 | golang结构体作为函数参数
Word plus watermark
HAL库的RCC简介
Test your machine learning pipeline
Flink SQL实现流批一体
分布式消息中间件框架选型-数字化架构设计(7)