L3-010. 是否完全二叉搜索树
L3-010. 是否完全二叉搜索树
将一系列给定数字顺序插入一个初始为空的二叉搜索树(定义为左子树键值大,右子树键值小),你需要判断最后的树是否一棵完全二叉树,并且给出其层序遍历的结果。
输入格式:
输入第一行给出一个不超过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 <iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<string>
#include<set>
#include<queue>
#include<deque>
#include<algorithm>
#include<cmath>
using namespace std; struct node
{
int num,left,right;
}tree[];
int a[];
int n; void build()
{
int len=;
tree[].num=a[];
for(int i=;i<=n;i++)
{ int j=;
while()
{
while(a[i]>tree[j].num)
{
if(tree[j].left==-) break;
j=tree[j].left;
}
if (a[i]>tree[j].num && tree[j].left==-)
{
tree[++len].num=a[i];
tree[j].left=len;
break;
} while(a[i]<tree[j].num)
{
if(tree[j].right==-) break;
j=tree[j].right;
}
if (a[i]<tree[j].num && tree[j].right==-)
{
tree[++len].num=a[i];
tree[j].right=len;
break;
} } }
}
void work()
{
queue<int> Q;
Q.push();
int flag=;
int k=;
while(!Q.empty())
{
int u=Q.front(); Q.pop();
printf("%d",tree[u].num);
k++;
if (k<n) printf(" "); else printf("\n");
if (flag>=)
{
if (tree[u].left> && flag>) flag=-;
else if (tree[u].left<) flag++;
}
if (flag>=)
{
if (tree[u].right> && flag>) flag=-;
else if (tree[u].right<) flag++;
} if (tree[u].left!=-) Q.push(tree[u].left);
if (tree[u].right!=-) Q.push(tree[u].right); }
if (flag==-) printf("NO\n");
else printf("YES\n");
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
tree[i].num=;
tree[i].left=-;
tree[i].right=-;
}
build();
work();
return ;
}
L3-010. 是否完全二叉搜索树的更多相关文章
- PAT天梯赛练习题 L3-010. 是否完全二叉搜索树(完全二叉树的判断)
L3-010. 是否完全二叉搜索树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 将一系列给定数字顺序插入一个初始为空的二叉搜 ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- [LeetCode] Convert Sorted List to Binary Search Tree 将有序链表转为二叉搜索树
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...
- [LeetCode] Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [LeetCode] Validate Binary Search Tree 验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
随机推荐
- TOSCA自动化测试工具--How to modify windows
1.页面窗口(高亮的部分是我们需要的所有窗口) 2.窗口可以任意拖拽到任何地方 3.窗口可以并列显示 任务栏点击按钮可以继续拖动 放到自己想放的地方 4.收起preview,调整宽窄 5.保存当前wi ...
- 如何在idea中设置 jsp 内容修改以后,立即生效而不用重新启动服务?
点击 run---->edit configuration--->
- ELK日志系统
ELK stack是又Elasticsearch,lostash,kibana 三个开源软件的组合而成,形成一款强大的实时日志收集分析展示系统. Logstash:日志收集工具,可以从本地磁盘,网络服 ...
- win7_32下编译FFmpeg
运行环境: VC2010软件: [附:本文所用软件安装包:http://download.csdn.NET/detail/sinat_36666600/9705438 ...
- 20145303刘俊谦 《Java程序设计》实验四 实验报告
实验要求 完成实验.撰写实验报告,实验报告以博客方式发表在博客园,注意实验报告重点是运行结果,遇到的问题(工具查找,安装,使用,程序的编辑,调试,运行等).解决办法(空洞的方法如"查网络&q ...
- Java学习第一周博客
20145307<Java程序设计>第一周学习总结 教材学习内容总结 首先学习安装Java有两种方法,一种是用Eclipse直接编辑输出,另一种方法是用记事本之后用win+G开启cmd运行 ...
- 6.scala中的包
版权申明:转载请注明出处. 文章来源:http://bigdataer.net/?p=287 排版乱?请移步原文获得更好的阅读体验 1.基础特性 scala中的包和java中的包类似,都是用来在大型工 ...
- 高并发下,php与redis实现的抢购、秒杀功能
抢购.秒杀是如今很常见的一个应用场景,主要需要解决的问题有两个: 1 高并发对数据库产生的压力 2 竞争状态下如何解决库存的正确减少("超卖"问题) 对于第一个问题,已经很容易想到 ...
- codeforces 578c - weekness and poorness - 三分
2017-08-27 17:24:07 writer:pprp 题意简述: • Codeforces 578C Weakness and poorness• 给定一个序列A• 一个区间的poornes ...
- python 贪婪和非贪婪模式
这样的正则表达式: r'\*(.+)\*' 如果想要匹配*something*这样的一个串按道理说是没问题的 但是如果文本是*this* is *something* 那么我们的正则表达式就会采取贪 ...