当前位置:网站首页>Complete binary search tree (30 points)
Complete binary search tree (30 points)
2022-04-23 08:41:00 【Huaihua second affectionate】
Insert a series of given numbers into a binary search tree which is initially empty ( It is defined as the left subtree with large key value , The key value of the right subtree is small ), You need to determine whether the final tree is a complete binary tree , And the result of sequence traversal is given .
Input format :
The first line of input gives no more than one 20 The positive integer N; The second line gives N Two different positive integers , Separated by spaces .
Output format :
Will input N Insert an initially empty binary search tree in the order of positive integers . Output the sequence traversal result of the result tree in the first line , Between the numbers 1 Space separation , There must be no extra Spaces at the beginning or end of the line . The second line outputs YES, If the tree is a complete binary tree ; Otherwise output NO.
sample input 1:
9
38 45 42 24 58 30 67 12 51
sample output 1:
38 45 24 58 42 30 12 67 51
YES
sample input 2:
8
38 24 12 45 58 67 42 51
sample output 2:
38 45 24 58 42 12 67 51
NO
#include<bits/stdc++.h>
using namespace std;
vector<int> level(100010, -1);
int n, t, flag, st, cnt;
void build(int val, int root){
if(val > level[root]){
if(level[2 * root + 1] != -1) build(val, 2 * root + 1);
else level[2 * root + 1] = val;
}
else if(val < level[root]){
if(level[2 * root + 2] != -1) build(val, 2 * root + 2);
else level[2 * root + 2] = val;
}
}
int main(){
cin >> n >> t, level[0] = t;
for(int i = 1;i < n;i++) cin >> t, build(t, 0);
for(int i = 0;i < 100010;i++){
if(level[i] != -1){
if(flag++) cout << " ";
cout << level[i];
cnt++;
}else{
if(cnt < n) st = 1;
}
}
if(st) cout << endl << "NO";
else cout << endl << "YES";
}
版权声明
本文为[Huaihua second affectionate]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230835222674.html
边栏推荐
- php基于哈希算法出现的强弱比较漏洞
- 引用传递1
- Use of Arthas in JVM tools
- 请问中衍期货安全靠谱吗?
- ELK生产实践
- LINQ Learning Series ----- 1.4 anonymous objects
- input元素添加监听事件
- Transformer XL: attention language modelsbbeyond a fixed length context paper summary
- Yangtao electronic STM32 Internet of things introduction 30 steps notes 1. The difference between Hal library and standard library
- Failed to convert a NumPy array to a Tensor(Unsupported Object type int)
猜你喜欢

请提前布局 Star Trek突破链游全新玩法,市场热度持续高涨

excle加水印

pgsql想实现mysql一样样的列子查询操作

四张图弄懂matplotlib的一些基本用法

PgSQL wants to implement all kinds of column sub query operations of MySQL

After a circle, I sorted out this set of interview questions..

HAL库的RCC简介

作文以记之 ~ 二叉树的后序遍历

Notes on 30 steps of introduction to the Internet of things of yangtao electronics STM32 III. cubemx graphical programming and setting the IO port on the development board

GUI编程简介 swing
随机推荐
uni-app和微信小程序中的getCurrentPages()
word加水印
How browser works
DOM学习笔记---遍历页面所有元素节点
Go语言自学系列 | golang结构体的初始化
Introduction to protobuf
dataBinding中使用include
xctf刷题小记
洋桃电子STM32物联网入门30步笔记三、CubeMX图形化编程、设置开发板上的IO口
RPC procedure
DOM learning - add + - button
[explanation] get ora-12838: cannot read / modify an object after modifying it in parallel
请提前布局 Star Trek突破链游全新玩法,市场热度持续高涨
RCC introduction of Hal Library
记录:js删除数组中某一项或几项的几种方法
LaTeX数学公式
form中enctype属性
耳穴减肥自身感受细节描述0422
rembg 分割mask
Misunderstanding of flush () method of OutputStream class