PAT甲题题解-1110. Complete Binary Tree (25)-(判断是否为完全二叉树)
题意:判断一个节点为n的二叉树是否为完全二叉树。Yes输出完全二叉树的最后一个节点,No输出根节点。
建树,然后分别将该树与节点树为n的二叉树相比较,统计对应的节点个数,如果为n,则为完全二叉树,否则即不是。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h> using namespace std;
const int maxn=;
int two[];
int lastNode;
struct Node{
int left,right;
int id;
}tree[maxn],cbt[maxn]; /*
将输入给的二叉树和节点为n的完全二叉树一个个作比较,计算对应的节点个数
如果节点个数为n,则二叉树为完全二叉树。
i为完全二叉树的当前节点编号。
root为输入二叉树的对应节点。
*/
int compare(int root,int i,int n){
int res1=-,res2=-;
if(root==-){
return ;
}
if(i==n)
lastNode=root; //记录完全二叉树的最后一个节点的id
int sum=; if(*i<=n){
sum+=compare(tree[root].left,i*,n);
}
if(*i+<=n){
sum+=compare(tree[root].right,i*+,n);
}
bool flag1=false,flag2=false;
if((*i<=n&&tree[root].left!=-)||(*i>n&&tree[root].left==-))
flag1=true;
if((*i+<=n&&tree[root].right!=-)||(*i+>n&&tree[root].right==-))
flag2=true;
if(flag1 && flag2)
sum++;
return sum;
}
int main()
{
int n;
scanf("%d",&n);
char str1[],str2[];
int a;
int vis[maxn];
memset(vis,-,sizeof(vis));
for(int i=;i<n;i++){
scanf("%s %s",str1,str2);
tree[i].id=i;
if(str1[]=='-')
tree[i].left=-;
else{
a=atoi(str1);
tree[i].left=a;
vis[a]=;
}
if(str2[]=='-')
tree[i].right=-;
else{
a=atoi(str2);
tree[i].right=a;
vis[a]=;
}
}
int root;
for(int i=;i<n;i++){
if(vis[i]==-)
root=i;
} if(compare(root,,n)==n){
printf("YES %d",lastNode);
}
else{
printf("NO %d",root);
}
return ;
}
该博客里的解题方法要比我好一点,即按照层次遍历该二叉树,每遍历一个节点cnt++,直到为-1。此时cnt=n,则Yes,否则No。
http://www.liuchuo.net/archives/2158
PAT甲题题解-1110. Complete Binary Tree (25)-(判断是否为完全二叉树)的更多相关文章
- PAT甲题题解-1064. Complete Binary Search Tree (30)-中序和层次遍历,水
由于是满二叉树,用数组既可以表示父节点是i,则左孩子是2*i,右孩子是2*i+1另外根据二分搜索树的性质,中序遍历恰好是从小到大排序因此先中序遍历填充节点对应的值,然后再层次遍历输出即可. 又是一道遍 ...
- [二叉树建树&完全二叉树判断] 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 ...
- 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 ...
- 1110. Complete Binary Tree (25)
Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...
- 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 Level) 1110. Complete Binary Tree (25)
判断一棵二叉树是否完全二叉树. #include<cstdio> #include<cstring> #include<cmath> #include<vec ...
- 【PAT甲级】1110 Complete Binary Tree (25分)
题意: 输入一个正整数N(<=20),代表结点个数(0~N-1),接着输入N行每行包括每个结点的左右子结点,'-'表示无该子结点,输出是否是一颗完全二叉树,是的话输出最后一个子结点否则输出根节点 ...
- PAT甲题题解-1012. The Best Rank (25)-排序水题
排序,水题因为最后如果一个学生最好的排名有一样的,输出的课程有个优先级A>C>M>E那么按这个优先级顺序进行排序每次排序前先求当前课程的排名然后再与目前最好的排名比较.更新 至于查询 ...
- PAT甲题题解-1036. Boys vs Girls (25)-找最大最小,大水题
题意:给出n个人的姓名.性别.ID.分数,让你找出其中哪个妹纸分数最高.哪个汉子分数最低.以及他们的差如果没有妹纸或者汉子,则对应输出Absent,差用NA代替. 就是for一遍找最大最小值,水题 # ...
随机推荐
- Beta阶段第五次冲刺
Beta阶段第五次冲刺 严格按照Git标准来,组员有上传Git的才有贡献分没有的为0 代码签入图 1.part1 -站立式会议照片 2.part2 -项目燃尽图 3.part3 -项目进展 1.正在进 ...
- Vue图片懒加载
图片懒加载的原理 先将img标签中的src链接设为同一张图片(空白图片),将其真正的图片地址存储再img标签的自定义属性中(比如data-src).当js监听到该图片元素进入可视窗口时,即将自定义属性 ...
- 宿主在Windows Service中的WCF(创建,安装,调用) (host到exe,非IIS)
1. 创建WCF服务 在vs2010中创建WCF服务应用程序,会自动生成一个接口和一个实现类:(IService1和Service1) IService1接口如下: using System.Ru ...
- pyspider爬取数据存入redis--2.测试数据库连通性
直接上代码 #!/usr/bin/env python # -*- encoding: utf-8 -*- # Created on 2017-10-27 09:56:50 # Project: re ...
- 如何利用Grunt生成对应的Source Map文件,线上代码压缩使用chrome浏览器便于调式
如何利用Grunt生成对应的Source Map文件,线上代码压缩使用chrome浏览器便于调式 首先我们来说说为何要生成sourceMap文件呢?简单的说,sourceMap是为了压缩后的代码调式提 ...
- uC/OS-III 时钟节拍,时间管理,时间片调度
uC/OS-III 时钟节拍,时间管理,时间片调度 时钟节拍 时钟节拍可谓是 uC/OS 操作系统的心脏,它若不跳动,整个系统都将会瘫痪. 时钟节拍就是操作系统的时基,操作系统要实现时间上的管理, ...
- 第11章 GPIO输出—使用固件库点亮LED
第11章 GPIO输出—使用固件库点亮LED 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku. ...
- ubuntu 下 go 语言调试器 dlv 的安装
1. 从 https://github.com/derekparker/delve.git 下载delve压缩包delve-master.zip. 2. 使用 winscp 工具将delve-mas ...
- Vultr搭建SS服务
购买VPS VPS又叫虚拟服务器,相当于是讲物理服务器的资源进行虚拟划分然后分配给不同的用户使用. Vultr服务器按小时计费,最低0.004美元/h,算起来2.5美元/月,且destory掉服务器是 ...
- Android开发——JobScheduler机制
年Google开发大会上指出,如果每个APP都使用这个API,那么可以节约15%到20%的电量. 2. JobScheduler拥有更多的触发选项 JobScheduler比AlarmManager ...