团体程序设计天梯赛L3-010 是否完全二叉搜索树 2017-03-24 16:12 29人阅读 评论(0) 收藏
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<map>
#include<set>
#include<ctime>
#include<cmath>
#include<queue>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define inf 0x3f3f3f3f int tree[10000000];
int n,mx; void build(int x)
{
int pos=1;
while(tree[pos]!=-1)
{
if(x>tree[pos])
pos*=2;
else
pos=pos*2+1;
}
tree[pos]=x;
mx=max(mx,pos);
} int main()
{
int a[100];
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
memset(tree,-1,sizeof tree);
mx=0;
for(int i=0;i<n;i++)
{
build(a[i]);
}
int cnt=0,q=0;
for(int i=1;i<=mx;i++)
{
if(tree[i]!=-1)
{
if(q++)
printf(" ");
printf("%d",tree[i]);
}
else
{
cnt++;
} }
printf("\n");
if(cnt==0)
printf("YES\n");
else
printf("NO\n"); return 0;
}
数组模拟链表:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std; struct node
{ int data,lch,rch; } tree[100];
int n,pos;
void build(int a)
{
int k=0;
while(1)
{
if(a>tree[k].data)
{
if(tree[k].lch==-1)
{
tree[++pos].data=a;
tree[k].lch=pos;
break;
}
else
{
k=tree[k].lch;
}
}
else
{
if(tree[k].rch==-1)
{
tree[++pos].data=a;
tree[k].rch=pos;
break;
}
else
{
k=tree[k].rch;
}
}
}
} void bfs()
{
queue<int>q;
q.push(0);
int f;
int qq=0;
int flag=0,fl=0;
while(!q.empty())
{
f=q.front();
q.pop();
if(f==-1)
{
flag=1;
}
else
{
if(qq++)
printf(" ");
printf("%d",tree[f].data);
q.push(tree[f].lch);
q.push(tree[f].rch);
if(flag==1)
fl=1;
}
}
if(fl==1)
printf("\nNO\n");
else
printf("\nYES\n");
} int main()
{
int a;
scanf("%d",&n);
memset(tree,-1,sizeof tree);
pos=0;
scanf("%d",&tree[0].data);
for(int i=1; i<n; i++)
{
scanf("%d",&a);
build(a);
}
bfs(); return 0;
}
团体程序设计天梯赛L3-010 是否完全二叉搜索树 2017-03-24 16:12 29人阅读 评论(0) 收藏的更多相关文章
- 团体程序设计天梯赛L2-002 链表去重 2017-03-22 18:12 25人阅读 评论(0) 收藏
L2-002. 链表去重 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一个带整数键值的单链表L,本题要求你编写程序,删除 ...
- PAT天梯赛练习题 L3-010. 是否完全二叉搜索树(完全二叉树的判断)
L3-010. 是否完全二叉搜索树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 将一系列给定数字顺序插入一个初始为空的二叉搜 ...
- 团体程序设计天梯赛L2-021 点赞狂魔 2017-04-18 11:39 154人阅读 评论(0) 收藏
L2-021. 点赞狂魔 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 微博上有个"点赞"功能,你可以为你 ...
- 团体程序设计天梯赛L2-024 部落 2017-04-18 11:31 274人阅读 评论(0) 收藏
L2-024. 部落 时间限制 120 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 在一个社区里,每个人都有自己的小圈子,还可能同时属于很多不 ...
- 团体程序设计天梯赛L2-023 图着色问题 2017-04-17 09:28 269人阅读 评论(0) 收藏
L2-023. 图着色问题 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 图着色问题是一个著名的NP完全问题.给定无向图 G ...
- 团体程序设计天梯赛L1-020 帅到没朋友 2017-03-22 17:46 72人阅读 评论(0) 收藏
L1-020. 帅到没朋友 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 当芸芸众生忙着在朋友圈中发照片的时候,总有一些人因为 ...
- 团体程序设计天梯赛L1-019 谁先倒 2017-03-22 17:35 33人阅读 评论(0) 收藏
L1-019. 谁先倒 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 划拳是古老中国酒文化的一个有趣的组成部分.酒桌上两人划拳 ...
- 团体程序设计天梯赛L1-017 到底有多二 2017-03-22 17:31 155人阅读 评论(0) 收藏
L1-017. 到底有多二 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 一个整数"犯二的程度"定义为该数 ...
- 团体程序设计天梯赛L1-018 大笨钟 2017-03-22 17:29 79人阅读 评论(0) 收藏
L1-018. 大笨钟 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 微博上有个自称"大笨钟V"的家伙,每 ...
随机推荐
- SQLServer数据库优化常用语句
-- 平均物理读次数最多的SQL语句:select top 50 *, (s.total_physical_reads / s.execution_count) as avephysicalreads ...
- react.js的了解
React很大的特点就是“轻”,再加上VDOM这个很好的idea让React非常非常快(在上面那个测试里面0.3s左右就载入完毕).另外React和Angular一个很大的不同就是React采用的是o ...
- OD 实验(十三) - 对一个程序的逆向
程序: 运行程序 点击 Start,它就会进行对系统的扫描 点击 About -> Enter Registration Code 随便输入一下内容,点击 OK,会弹出该弹窗 用 PEiD 看一 ...
- 微软TechNet关于TLS的细节的描述
https://technet.microsoft.com/en-us/library/cc785811.aspx TLS协议太复杂了,RFC太长没时间看,这篇还可以,好歹知道个大概. 想知道全部细节 ...
- ZooKeeper与仲裁模式
为了让服务器之间可以通信,服务器间需要一些联系信息.理论上,服务器可以使用多播来发现彼此,但我们想让ZooKeeper集合支持跨多个网 络而不是单个网络,这样就可以支持多个集合的情况. 为了完成这些, ...
- leetcode289
public class Solution { public void GameOfLife(int[][] board) { ) - ; ].GetLength() - ; var list = n ...
- Maven中dependencyManagement使用
在Maven中dependencyManagement的作用其实相当于一个对所依赖jar包进行版本管理的管理器. 在dependencyManagement下申明的dependencies,Maven ...
- TortoiseSVN文件夹及文件图标不显示解决方法(兼容Window xp、window7)
最近遇到TortoiseSVN图标(如上图:增加文件图标.文件同步完成图标等)不显示问题,网上找到的解决方法试了很多都无法真正解决,最后总结了一下,找到了终极解决方案,当然此方案也有弊端,接下来我们就 ...
- C# XML文件读取
using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; usin ...
- 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 14—Dimensionality Reduction 降维
Lecture 14 Dimensionality Reduction 降维 14.1 降维的动机一:数据压缩 Data Compression 现在讨论第二种无监督学习问题:降维. 降维的一个作用是 ...