L3-010. 是否完全二叉搜索树

时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
陈越

将一系列给定数字顺序插入一个初始为空的二叉搜索树(定义为左子树键值大,右子树键值小),你需要判断最后的树是否一棵完全二叉树,并且给出其层序遍历的结果。

输入格式:

输入第一行给出一个不超过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. 是否完全二叉搜索树的更多相关文章

  1. PAT天梯赛练习题 L3-010. 是否完全二叉搜索树(完全二叉树的判断)

    L3-010. 是否完全二叉搜索树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 将一系列给定数字顺序插入一个初始为空的二叉搜 ...

  2. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  3. [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  4. [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 ...

  5. [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 ...

  6. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  7. [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 ...

  8. [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. 这道 ...

  9. [LeetCode] Recover Binary Search Tree 复原二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  10. [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 ...

随机推荐

  1. JQuery归纳总结(增加中...)

    一.this与$(this)的区别 this指向标签本身对象,而$(this)会将其封装成JQuery对象 如: $(" img").mousemove( function(e){ ...

  2. 20145316《Java程序设计》第七周学习总结

    20145316<Java学习程序设计>第七周学习总结 教材学习知识总结 1.在只有Lambda表达式的情况下,参数的类型必须写出来. 2.Lambda表达式本身是中性的,同样的Lambd ...

  3. asp.net Mvc 使用uploadify 上传文件 HTTP 302 Error

    CSHTML代码 @{ if (Request.Cookies[FormsAuthentication.FormsCookieName] != null) { <input type=" ...

  4. git操作方便,简单使用的客户端sourcetree 安装配置所遇问题总结

    常言道:工欲善其事,必先利其器. SourceTree是老牌的Git GUI管理工具了,也号称是最好用的Git GUI工具 这里先言言它的好: * 免费 * 功能强大:无论你是新手还是重度用户,Sou ...

  5. linux下nano中复制粘贴剪切的快捷键是什么

    答: 1.复制:alt+6 2.剪切:ctrl+k 3.粘贴:ctrl+u 4.自由剪切: ctrl+6指定起始剪切位置,按上下左右键来选中内容,然后按下ctrl+k即可自由剪切 5.撤销: alt+ ...

  6. ubuntu下源码安装wget

    1.背景 ubuntu18.04 64bit 2.安装方法如下: 2.1.获取源码 curl -o wget-1.20.tar.gz ftp://ftp.gnu.org/gnu/wget/wget-1 ...

  7. MySQL——修改数据表

    1.添加单列: ALERT TABLE tbl_name ADD [COLUMN] col_name column_definition [FIRST|AFTER col_name 其中tbl_nam ...

  8. [CF914D]Bash and a Tough Math Puzzle

    给定一个数列$a_1,a_2,...,a_n$,支持两种操作 1 l r x,猜测数列中[l,r]位置上的数的最大公约数$x$,判断这个猜测是否是接近正确的.如果我们可以在数列[l,r]位置中改动至多 ...

  9. JasperReports实现报表调出excel

    一.利用工具iReport 创建task.jrxml 模板 并生成 task.jasper 文件 二.搭建工程导入以下jar包 commons-beanutils-1.9.2.jar commons- ...

  10. Anaconda 环境中使用pip安装时候出现的一些问题

    author:pprp date:18/8/12 --- 1. AttributeError: Module Pip has no attribute 'main' solution:降低pip的版本 ...