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 ...
随机推荐
- Jquery Easy UI Datagrid 上下移动批量保存数据
DataGrid with 上下移动批量保存数据 通过前端变量保存修改数据集合,一次性提交后台执行 本想结合easyui 自带的$('#dg').datagrid('getChanges'); 方法来 ...
- javascript 理解对象--- 定义多个属性和读取属性的特性
一 定义多个属性 ECMAScript5 定义了一个Object.defineProperties()方法,用于定义多个属性.此方法接受两个对象参数: 第一个对象:要添加或修改其属性的对象 第二个对象 ...
- cocos2d: fullPathForFilename: No file found at /cc_2x2_white_image. Possible missing file.
程序运行的时候输出这条信息cocos2d: fullPathForFilename: No file found at /cc_2x2_white_image. Possible missing fi ...
- PHP联接MySQL
<?php echo "This is a test</br>"; echo "asdfasdfadsf"; $mysql_server_na ...
- Printf的缓冲机制
转:https://blog.csdn.net/qq_25424545/article/details/78772959 今天用fork()写程序时候,突然发现自己对Printf的缓冲机制还是有些不够 ...
- 【Python】装饰器 & 偏函数
[装饰器] 1.最简单的Decorator. def author(f): def addName(): print('My name is xkfx.\n') f() return addName ...
- s3cmd安装
配置yum.repos cd /etc/yum.repos.d/ vim s3tools.repo [s3tools] name=Tools for managing Amazon S3 - Simp ...
- GET 对比 POST
HTTP 方法:GET 对比 POST HTTP 消息 标签列表(字母排序) 两种最常用的 HTTP 方法是:GET 和 POST. 什么是 HTTP? 超文本传输协议(HTTP)的设计目的是保证客户 ...
- spring boot开发为什么使用jar包
spring boot既可以打成war发布,也可以找成jar包发布. jar包:直接通过内置tomcat运行,不需要额外安装tomcat.如需修改内置tomcat的配置,只需要在spring boot ...
- LeetCode——Single Element in a Sorted Array
Question Given a sorted array consisting of only integers where every element appears twice except f ...