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. spark restful 作业提交

    spark1.4起,在启动master进程时候,同时会有一个restful的服务器,可以接受RESTFUL的请求, 以下是提交应用的示例 curl -X POST http://tssloginsig ...

  2. Centos上安装python3.5以上版本

    一.准备工作: yum install zlib-devel yum install openssl-devel 二.安装python3.5 wget https://www.python.org/f ...

  3. Django 部署(Apache下)

    前言: 因为需要在服务器下运行python脚本,所以需要搭建Django服务器.所以将自己的学习过程也记录下来,方便日后查阅. 本文环境如下: Ubuntu 16.04  python2.7 Apac ...

  4. 如何建立DB2分区数据库?(转)

    欢迎和大家交流技术相关问题:邮箱: jiangxinnju@163.com博客园地址: http://www.cnblogs.com/jiangxinnjuGitHub地址: https://gith ...

  5. iOS 所有设备一览 && CoreFoundation源码

    1. 所有设备一览 https://en.wikipedia.org/wiki/List_of_iOS_devices 2. CoreFoundation源码(可以看看runloop.runtime的 ...

  6. Promise原理剖析

    传统的异步回调编程最大的缺陷是:回调地狱,由于业务逻辑非常复杂,代码串行请求好几层:并行请求以前也要通过引用step.async库实现.现在ES6推出了Promise,通过Promise的链式调用可以 ...

  7. 20145331 《Java程序设计》第2周学习总结

    20145331<Java程序设计>第2周学习总结 教材学习内容总结 3.1 类型.变量与运算符 •注释://(单行注释).//(多行注释)./ */(javadoc文档注释 )注释的内容 ...

  8. SaltStack部署服务及配置管理apache+php-第二篇

    实验目标 1.使用SaltStack部署apache和php, 2.使用salt管理httpd.conf配置文件配置访问info.php使用账户密码 3.在salt里面增加对conf.d目录进行配置管 ...

  9. svn的分支

    svn的分支使用 新建一个项目的时候,选择建立自带trunk,branches和tags文件夹的. 其中trunk作为主开发. 有需要的时候,从trunk创建分支到对应的branches下面,新建分支 ...

  10. Nginx+vsftpd搭建图片服务器

    安装Nginx 参考:http://www.cnblogs.com/idefav2010/p/nginx-concat.html Nginx配置文件 location ~ .*\.(gif|jpg|j ...