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 ...
随机推荐
- SSIS 2012 Error: An Integration Services class cannot be found
升级SSIS到SQL Server 2012,服务器只安装了SSIS一个功能,应用程序执行dtsx包时报错如下: An Integration Services class cannot be fou ...
- Struts2.5.12中动态方法调用问题
使用版本:struts-2.5.12-all 出现问题:在开启动态方法调用后,找不到没有匹配的路径映射 <constant name="struts.enable.DynamicMet ...
- 前端JS复制特定区域的文本(兼容safari)
html5的webAPI接口可以很轻松的使用短短的几行代码就实现点击按钮复制区域文本的功能,不需要依赖flash. 代码如下: /* 创建range对象 */ const range = docume ...
- Web安全学习笔记之Nmap脚本编写
0x00 Nmap脚本简介 夜无眠,看了一下Nmap官方的英文API文档(全是English),瞬间心态崩塌,不想吐槽它们的nmap官网前端太丑了=.=,但是都是大牛啊,挺敬佩开源开发者的. Nmap ...
- APP接口版本不兼容怎么办? 教你一招
现在基本每个公司都做APP,所以大家都面临 APP接口版本兼容的问题. iOS和android 要不断开发新版本,很多服务端开发都是在以前接口的逻辑上进行修改.新的APP和接口开发后,接口如何兼容老的 ...
- 20145335郝昊《java程序设计》第5周学习总结
20145335郝昊<Java程序设计>第5周学习总结 教材学习内容总结 第八章 语法与继承架构 使用try.catch 特点: - 使用try.catch语法,JVM会尝试执行try区块 ...
- linux字典生成工具crunch
安装 tar zxvf crunch-3.6.tgz cd crunch-3.6 gcc -Wall -lm -pthread -std=c99 -m64 -D_LARGEFILE_SOURCE -D ...
- Docker storage drivers
因为Docker的镜像是分层的,包含只读层和可读写层,因此选择正确的Storage Driver对于容器的性能是非常重要的. 支持的Linux发行版本 目前推荐使用性能最好的Overlay2.RHEL ...
- linux下如何使用自己安装的SunJDK替换默认的OpenJDK
在linux系统中,由于涉及到版权问题,在大部分linux系统的发行版本中,默认都安装了OpenJDK,并且OpenJDK的java命令也已经加入到环境变量中了. 在刚装好的linux系统中,运行ja ...
- SQL Server-深入剖析统计信息
转自: http://www.cnblogs.com/zhijianliutang/p/4190669.html 概念理解 关于SQL Server中的统计信息,在联机丛书中是这样解释的 查询优化 ...