团体程序设计天梯赛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"的家伙,每 ...
随机推荐
- 利用JAVA操作Redis---demo
package com.js.ai.modules.pointwall.interfac; import java.util.HashMap; import java.util.Iterator; i ...
- 在使用Ajax请求返回json数据的时候IE浏览器弹出下载保存对话框的解决方法
在使用Ajax请求返回json数据的时候IE浏览器弹出下载保存对话框的解决方法 最近在做一个小东西,使用kindeditor上传图片的时候,自己写了一个上传的方法,按照协议规则通过ajax返回json ...
- poj 3790 Recursively Palindromic Partitions
/*摘抄自博客:Recursively Palindromic Partitions Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- Java Thread 多线程同步、锁、通信
参看:http://www.cnblogs.com/hoojo/archive/2011/05/05/2038101.html
- Spring中application*的使用
ApplicationAware 加载Spring配置文件时,如果Spring配置文件中所定义的Bean类实现了ApplicationContextAware 接口,那么在加载Spring配置文件时, ...
- 代做JSP课程设计,毕业设计
代做JSP课程设计,毕业设计,大家都是学生,绝对靠谱,有意者加我Q 279283855
- Tkinter tkMessageBox
Tkinter tkMessageBox: tkMessageBox模块用于显示在您的应用程序的消息框.此模块提供了一个功能,您可以用它来显示适当的消息 tkMessageBox模块 ...
- 05_java之方法
01方法的概述 * A: 为什么要有方法 * 提高代码的复用性 * B: 什么是方法 * 完成特定功能的代码块. 02方法的定义格式 * A: 方法的格式 * 修饰符 返回值类型 方法名(参数类型 参 ...
- django admin 中实现word文档下载
为了实现此功能,需要用到的知识点: (1)django admin 自定义字段: 参考:http://www.cnblogs.com/wumingxiaoyao/p/6928297.html ...
- LeetCode之位操作题java
191. Number of 1 Bits Total Accepted: 87985 Total Submissions: 234407 Difficulty: Easy Write a funct ...