当前位置:网站首页>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
边栏推荐
- jsp页面编码
- SYS_ CONNECT_ BY_ Path (column, 'char') combined with start with connect by prior
- 队列(c语言/链表)
- Word plus watermark
- php基于哈希算法出现的强弱比较漏洞
- word加水印
- Asan minimalism
- ELK生产实践
- Introduction to protobuf
- Idea is configured to connect to the remote database mysql, or Navicat fails to connect to the remote database (solved)
猜你喜欢

Transformer XL: attention language modelsbbeyond a fixed length context paper summary

Input / output system

经典题目刷一刷

洋桃电子STM32物联网入门30步笔记二、CubeIDE下载、安装、汉化、设置

Noyer électronique stm32 Introduction à l'Internet des objets 30 étapes notes I. différences entre la Bibliothèque Hal et la Bibliothèque standard

Reference passing 1

第一性原理 思维导图

【深度好文】Flink SQL流批⼀体化技术详解(一)

洋桃电子STM32物联网入门30步笔记三、新建CubeIDE工程和设置讲解

Redis Desktop Manager for Mac(Redis可视化工具)
随机推荐
STM32使用HAL库,整体结构和函数原理介绍
Yangtao electronic STM32 Internet of things introduction 30 steps notes 1. The difference between Hal library and standard library
应纳税所得额
Large amount of data submitted by form post
Virtual online exhibition - Online VR exhibition hall realizes 24h immersive exhibition viewing
CGM optimizes blood glucose monitoring and management -- Yiyu technology appears in Sichuan International Medical Exchange Promotion Association
Add listening event to input element
让地球少些“碳”息 度能在路上
关于堆的判断 (25 分) 两种插入方式
IDEA导入commons-logging-1.2.jar包
虚拟线上展会-线上vr展馆实现24h沉浸式看展
Yangtao electronic STM32 Internet of things entry 30 step notes IV. engineering compilation and download
《深度学习》学习笔记(八)
Copy array in JS
汇编语言与逆向工程 栈溢出漏洞逆向分析实验报告
flask项目跨域拦截处理以及dbm数据库学习【包头文创网站开发】
K210学习笔记(二) K210与STM32进行串口通信
'bully' Oracle enlarged its move again, and major enterprises deleted JDK overnight...
耳穴减肥自身感受细节描述0422
Noyer électronique stm32 Introduction à l'Internet des objets 30 étapes notes I. différences entre la Bibliothèque Hal et la Bibliothèque standard