1110 Complete Binary Tree (25 分)
Given a tree, you are supposed to tell if it is a complete binary tree.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤20) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N−1. Then N lines follow, each corresponds to a node, and gives the indices of the left and right children of the node. If the child does not exist, a -will be put at the position. Any pair of children are separated by a space.
Output Specification:
For each case, print in one line YES and the index of the last node if the tree is a complete binary tree, or NO and the index of the root if not. There must be exactly one space separating the word and the number.
Sample Input 1:
9
7 8
- -
- -
- -
0 1
2 3
4 5
- -
- -
Sample Output 1:
YES 8
Sample Input 2:
8
- -
4 5
0 6
- -
2 3
- 7
- -
- -
Sample Output 2:
NO 1 分析:判断是否是完全二叉树。先求根节点,可以设置一个数组child,如果输入过程中如果该结点被当做孩子节点那它一定不是根节点,输入完毕后从头开始遍历child数组,第一个是false的child[i]跳出,i就是根节点。那么如何判断是完全二叉树呢?这里提供两种方法。 1、层序遍历,每次把空节点也Push进队列中,完全二叉树层序遍历过程中,如果遇到空节点了,说明一定已经遍历完非空节点;而对非完全二叉树,如果遇到空节点了,后面还必定有非空节点。于是遍历过程中用一个变量统计已入队过的节点数量。
2、递归求最大下标值,完全二叉树中,最大下标值=最大结点数(起始为1); 而非完全二叉树中,最大下标值>最大结点数

代码分别如下:
/**
* Copyright(c)
* All rights reserved.
* Author : Mered1th
* Date : 2019-02-26-21.04.42
* Description : A1110
*/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<unordered_set>
#include<map>
#include<vector>
#include<set>
#include<queue>
using namespace std;
struct Node{
int l,r;
}node[];
]={false};
,n;
bool isCBT(int root){
;
queue<int>q;
q.push(root);
while(!q.empty()){
int top=q.front();
q.pop();
){
maxd=top;
cnt++;
q.push(node[top].l);
q.push(node[top].r);
}
else{
if(cnt==n) return true;
else return false;
}
}
}
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif
cin>>n;
string a,b;
;i<n;i++){
cin>>a>>b;
]=='-'){
node[i].l=-;
}
else{
node[i].l=stoi(a);
child[stoi(a)]=true;
}
]=='-'){
node[i].r=-;
}
else{
node[i].r=stoi(b);
child[stoi(b)]=true;
}
}
int root;
;i<n;i++){
if(child[i]==false){
root=i;
break;
}
}
if(isCBT(root)) printf("YES %d",maxd);
else printf("NO %d",root);
;
}
这里利用了二叉树静态存储的性质,即父节点和叶子节点之间的关系。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
struct node{
int data;
int l,r;
}Node[];
,n;
]={false};
/*void LayerOrder(int root){
queue<int> Q;
Q.push(root);
while(!Q.empty()){
int now=Q.front();
Q.pop();
if(Node[now].l!=-1){
if(Node[now].r!=-1){
cnt++;
Q.push(Node[now].l);
}
else{
cnt++;
}
}
if(Node[now].r!=-1){
if(Node[now].l!=-1){
cnt++;
Q.push(Node[now].r);
}
else{
cnt++;
}
}
}
}
*/
,ans;
void dfs(int root,int index){
//求出最大下标值,如果下标值等于最大节点数-1则说明是完全二叉树,若大于最大结点数-1则非完全二叉树
if(index>maxn){
maxn=index;
ans=root;
}
) dfs(Node[root].l,index*);
) dfs(Node[root].r,index*+);
}
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif
cin>>n;
string t1,t2;
;i<n;i++){
cin>>t1>>t2;
Node[i].data=i;
if(t1=="-"){
Node[i].l=-;
}
else{
Node[i].l=stoi(t1);
hashtable[stoi(t1)]=true;
}
if(t2=="-"){
Node[i].r=-;
}
else{
Node[i].r=stoi(t2);
hashtable[stoi(t2)]=true;
}
}
int i;
;i<n;i++){
if(hashtable[i]==false) break;
}
dfs(i,);
if(maxn==n){
cout<<"YES "<<ans;
}
else{
cout<<"NO "<<i;
}
;
}
1110 Complete Binary Tree (25 分)的更多相关文章
- 1110 Complete Binary Tree (25 分)
Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...
- 【PAT甲级】1110 Complete Binary Tree (25分)
题意: 输入一个正整数N(<=20),代表结点个数(0~N-1),接着输入N行每行包括每个结点的左右子结点,'-'表示无该子结点,输出是否是一颗完全二叉树,是的话输出最后一个子结点否则输出根节点 ...
- [二叉树建树&完全二叉树判断] 1110. Complete Binary Tree (25)
1110. Complete Binary Tree (25) Given a tree, you are supposed to tell if it is a complete binary tr ...
- 1110. Complete Binary Tree (25)
Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...
- PAT Advanced 1110 Complete Binary Tree (25) [完全⼆叉树]
题目 Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each ...
- PAT甲题题解-1110. Complete Binary Tree (25)-(判断是否为完全二叉树)
题意:判断一个节点为n的二叉树是否为完全二叉树.Yes输出完全二叉树的最后一个节点,No输出根节点. 建树,然后分别将该树与节点树为n的二叉树相比较,统计对应的节点个数,如果为n,则为完全二叉树,否则 ...
- PAT (Advanced Level) 1110. Complete Binary Tree (25)
判断一棵二叉树是否完全二叉树. #include<cstdio> #include<cstring> #include<cmath> #include<vec ...
- PAT甲级——1110 Complete Binary Tree (完全二叉树)
此文章同步发布在CSDN上:https://blog.csdn.net/weixin_44385565/article/details/90317830 1110 Complete Binary ...
- 1110 Complete Binary Tree
1110 Complete Binary Tree (25)(25 分) Given a tree, you are supposed to tell if it is a complete bina ...
随机推荐
- JAVA中int转string及String.valueOf()的使用
日常java开放中,经常会遇到int和String的互转,一般图省事的做法就是: String length = ""+100; length的生成需要使用两个临时字符串" ...
- SQL2012 创建备份计划
打开数据库,选择 管理 -> 新建维护计划,填写计划名称 修改计划参数 工具箱->备份数据库任务,拖到计划里 编辑任务 拖动清除数据库任务到计划 编辑清除任务 从备份任务到清除任务拖一个箭 ...
- 【Cocos2d-X(1.x 2.x) 修复篇】iOS6 中 libcurl.a 无法通过armv7s编译以及iOS6中无法正常游戏横屏的解决方法
本站文章均为李华明Himi原创,转载务必在明显处注明:转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/iphone-cocos2dx/1000.html ...
- opencv-python教程学习系列3-视频操作
前言 opencv-python教程学习系列记录学习python-opencv过程的点滴,本文主要介绍视频的获取和保存,坚持学习,共同进步. 系列教程参照OpenCV-Python中文教程: 系统环境 ...
- Mac下百度网盘破解
Mac版百度网盘破解 先下载正版的百度网盘 打开终端输入命令 cd ~/Downloads && git clone https://github.com/CodeTips/Baid ...
- Future接口和FutureTask类【FutureTask实现了Runnable和Future接口】
Future API: public interface Future<V> { /** * Attempts to cancel execution of this task. This ...
- eclipse 配置jdk和maven
准备工作:确保已安装好jdk和maven,并完全配置环境.若是没有请参考前两篇博客: jdk: http://www.cnblogs.com/qinbb/p/6861851.html maven ...
- MVC中未能加载程序集System.Web.Http/System.Web.Http.WebHost
==================================== 需要检查项目的Microsoft.AspNet.WebApi版本是否最新,System.Web.Http 这个命名空间需要更新 ...
- 实习第一周第一天:接口 extends是继承类,implement是实现接口,原接口里面的方法填充,方法名也是不变,重写override是父类的方法名不变,把方法体给改了
一.定义 Java接口(Interface),是一系列方法的声明,是一些方法特征的集合,一个接口只有方法的特征没有方法的实现,因此这些方法可以在不同的地方被不同的类实现,而这些实现可以具有不同的行为( ...
- [Python] 中文路径和中文文本文件乱码问题
情景: Python首先读取名为log.txt的文本文件, 其中包含有文件名相对路径信息filename. 随后Python调用shutil.copy2(src, dst)对该filename文件进行 ...
