当前位置:网站首页>完全二叉搜索树 (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
边栏推荐
猜你喜欢
随机推荐
uni-app和微信小程序中的getCurrentPages()
DOM learning - add + - button
队列(c语言/链表)
Add listening event to input element
Trust uses Tokio's notify and timeout to achieve the effect similar to the timeout condition variable
洋桃電子STM32物聯網入門30步筆記一、HAL庫和標准庫的區別
Enctype attribute in form
测试你的机器学习流水线
Description of the abnormity that the key frame is getting closer and closer in the operation of orb slam
Go语言自学系列 | golang方法
JVM工具之Arthas使用
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
Failed to convert a NumPy array to a Tensor(Unsupported Object type int)
Misunderstanding of flush () method of OutputStream class
Use of applicationreadyevent
vmware 搭建ES8的常见错误
经典题目刷一刷
Notes on English class (4)
Protobuf简介
【IndexOf】【lastIndexOf】【split】【substring】用法详解









