当前位置:网站首页>是否完全二叉搜索树 (30 分)
是否完全二叉搜索树 (30 分)
2022-04-23 08:35:00 【怀化第二深情】
将一系列给定数字顺序插入一个初始为空的二叉搜索树(定义为左子树键值大,右子树键值小),你需要判断最后的树是否一棵完全二叉树,并且给出其层序遍历的结果。
输入格式:
输入第一行给出一个不超过20的正整数N
;第二行给出N
个互不相同的正整数,其间以空格分隔。
输出格式:
将输入的N
个正整数顺序插入一个初始为空的二叉搜索树。在第一行中输出结果树的层序遍历结果,数字间以1个空格分隔,行的首尾不得有多余空格。第二行输出YES
,如果该树是完全二叉树;否则输出NO
。
输入样例1:
9
38 45 42 24 58 30 67 12 51
输出样例1:
38 45 24 58 42 30 12 67 51
YES
输入样例2:
8
38 24 12 45 58 67 42 51
输出样例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";
}
版权声明
本文为[怀化第二深情]所创,转载请带上原文链接,感谢
https://blog.csdn.net/dege2929512534/article/details/124339375
边栏推荐
- Consensus Token:web3.0生态流量的超级入口
- How browser works
- okcc呼叫中心外呼系统智能系统需要用多大的盘存录音?
- How to encrypt devices under the interconnection of all things
- Go语言自学系列 | golang结构体作为函数参数
- 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
- jsp页面编码
- QT reads all files under the path or files of the specified type (including recursion, judging whether it is empty and creating the path)
- Star Trek强势来袭 开启元宇宙虚拟与现实的梦幻联动
- 匿名類型(C# 指南 基礎知識)
猜你喜欢
Idea import commons-logging-1.2 Jar package
Shell script advanced
RPC过程
On time atom joins hands with oneos live broadcast, and the oneos system tutorial is fully launched
Star Trek强势来袭 开启元宇宙虚拟与现实的梦幻联动
idea打包 jar文件
Harbor企业级镜像管理系统实战
vmware 搭建ES8的常见错误
QT compilation qtxlsx Library
洋桃电子STM32物联网入门30步笔记三、新建CubeIDE工程和设置讲解
随机推荐
vmware 搭建ES8的常见错误
After a circle, I sorted out this set of interview questions..
STM32F103ZET6【标准库函数开发】----库函数介绍
RCC introduction of Hal Library
LeetCode-199-二叉树的右视图
RPC过程
DOM learning - add + - button
Use of applicationreadyevent
Reference passing 1
Go语言自学系列 | golang嵌套结构体
请提前布局 Star Trek突破链游全新玩法,市场热度持续高涨
Record: JS several methods to delete one or more items in the array
idea底栏打开services
洋桃電子STM32物聯網入門30步筆記一、HAL庫和標准庫的區別
完全二叉搜索树 (30 分)
bashdb下载安装
QT reading and writing XML files
《深度学习》学习笔记(八)
flask项目跨域拦截处理以及dbm数据库学习【包头文创网站开发】
What is RPC