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一遍找最大最小值,水题 # ...
随机推荐
- Docker容器学习与分享06
Docker容器网络 Docker有三种原生网络:none网络.host网络.bridge网络. 先来学习一下bridge网络. 首先使用ifconfig命令查看一下本机的网络设备: 从图中可以看见多 ...
- 1.Solr介绍
转载请出自出处:http://www.cnblogs.com/hd3013779515/ Solr是一个基于Lucene的全文搜索引擎,同时对其进行了扩展,提供了比Lucene更为丰富的查询语言,实现 ...
- console.time方法与console.timeEnd方法
在Node.js中,当需要统计一段代码的执行时间时,可以使用console.time方法与console.timeEnd方法,其中console.time方法用于标记开始时间,console.time ...
- JavaScript中数组slice和splice的对比小结
前言 今天重温了一下Javascript,看到了数组的方法,其中有两个比较相似的方法——splice和splice,看着很像,就是多了一个p,但是用法却相当不一样. 在使用中,可以通过选择一个具有强语 ...
- python 之 string() 模块
common string oprationsimport string1. string constants(常量) 1) string.ascii_letters The concat ...
- 【高德地图API】从零開始学高德JS API(四)搜索服务——POI搜索|自己主动完毕|输入提示|行政区域|交叉路口|自有数据检索
地图服务.大家能想到哪些?POI搜素,输入提示,地址解析,公交导航,驾车导航,步行导航,道路查询(交叉口),行政区划等等.假设说覆盖物Marker是地图的骨骼,那么服务,就是地图的气血. 有个各种各样 ...
- 小米3系统计算器自己定义开关控件-MySwitchView
1.前言 在android4.0以后,有switch控件.相似于iPhone上面滑块的效果.可是仅仅能用在4.0以后的系统中.之前的平台.就无法使用这种控件. 近段时间.看到了 ...
- Redis系列六:redis相关功能
一. 慢查询原因分析 与mysql一样:当执行时间超过阀值,会将发生时间耗时的命令记录 redis命令生命周期:发送 排队 执行 返回慢查询只统计第3个执行步骤的时间 预设阀值:两种方式,默认为10毫 ...
- Spring之强制修改某个方法的行为(Arbitrary method replacement)
A less commonly useful form of method injection than Lookup Method Injection is the ability to rep ...
- PHP百万级数据导出方案(多csv文件压缩)
本文转自网络仅供学习之用 概述: 最近公司项目要求把数据除了页面输出也希望有导出功能,虽然之前也做过几个导出功能,但这次数据量相对比较大,差不多一天数据就20W条,要求导7天或者30天,那么数据量就轻 ...