当前位置:网站首页>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
边栏推荐
- IDEA导入commons-logging-1.2.jar包
- Type anonyme (Principes fondamentaux du Guide c)
- JS中复制数组
- Use of Arthas in JVM tools
- Shell脚本进阶
- Redis master-slave server problem
- form表单 post提交 数据量大的问题
- 队列(c语言/链表)
- SYS_ CONNECT_ BY_ Path (column, 'char') combined with start with connect by prior
- Misunderstanding of flush () method of OutputStream class
猜你喜欢
随机推荐
IDEA导入commons-logging-1.2.jar包
MATLAB 画五星红旗
引用传递1
关于堆的判断 (25 分) 两种插入方式
jsp页面编码
正点原子携手OneOS直播 OneOS系统教程全面上线
Input / output system
LINQ学习系列-----1.4 匿名对象
ELK生产实践
STM32 uses Hal library. The overall structure and function principle are introduced
请问中衍期货安全靠谱吗?
Let the earth have less "carbon" and rest on the road
What is RPC
synchronized 锁的基本用法
RPC procedure
Go语言自学系列 | golang方法
经典题目刷一刷
求简单类型的矩阵和
idea打包 jar文件
Detailed description of self feeling of auricular point weight loss 0422









