题意:判断一个节点为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)-(判断是否为完全二叉树)的更多相关文章

  1. PAT甲题题解-1064. Complete Binary Search Tree (30)-中序和层次遍历,水

    由于是满二叉树,用数组既可以表示父节点是i,则左孩子是2*i,右孩子是2*i+1另外根据二分搜索树的性质,中序遍历恰好是从小到大排序因此先中序遍历填充节点对应的值,然后再层次遍历输出即可. 又是一道遍 ...

  2. [二叉树建树&完全二叉树判断] 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 ...

  3. 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 ...

  4. 1110. Complete Binary Tree (25)

    Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...

  5. 1110 Complete Binary Tree (25 分)

    Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...

  6. PAT (Advanced Level) 1110. Complete Binary Tree (25)

    判断一棵二叉树是否完全二叉树. #include<cstdio> #include<cstring> #include<cmath> #include<vec ...

  7. 【PAT甲级】1110 Complete Binary Tree (25分)

    题意: 输入一个正整数N(<=20),代表结点个数(0~N-1),接着输入N行每行包括每个结点的左右子结点,'-'表示无该子结点,输出是否是一颗完全二叉树,是的话输出最后一个子结点否则输出根节点 ...

  8. PAT甲题题解-1012. The Best Rank (25)-排序水题

    排序,水题因为最后如果一个学生最好的排名有一样的,输出的课程有个优先级A>C>M>E那么按这个优先级顺序进行排序每次排序前先求当前课程的排名然后再与目前最好的排名比较.更新 至于查询 ...

  9. PAT甲题题解-1036. Boys vs Girls (25)-找最大最小,大水题

    题意:给出n个人的姓名.性别.ID.分数,让你找出其中哪个妹纸分数最高.哪个汉子分数最低.以及他们的差如果没有妹纸或者汉子,则对应输出Absent,差用NA代替. 就是for一遍找最大最小值,水题 # ...

随机推荐

  1. 记录一次mysql使用load into命令导入csv格式数据的过程

    今天从qwiklab实验获取一组数据,大概有5万条,在qwiklab实验室使用的是pgsql数据库,但是今天想把他插入本地的mysql数据库中. 1.首先是查看一下数据内容: 数据中有的是空值,有的是 ...

  2. make报错

    笔记本Ubuntu16.04环境下,进入项目的src目录下执行make操作,发现报如下错误 /bin/sh: 1: /usr/bin/libtool: not found makefile:89: r ...

  3. Redis上踩过的一些坑

    来自: http://blog.csdn.net//chenleixing/article/details/50530419 上上周和同事(龙哥)参加了360组织的互联网技术训练营第三期,美团网的DB ...

  4. Tushare test

    查看版本 import tushare print(tushare.__version__) 1.2.12 初步的调用方法为: import tushare as ts ts.get_hist_dat ...

  5. Breaking Down Type Erasure in Swift

    Type Erasure Pattern We can use the type erasure pattern to combine both generic type parameters and ...

  6. 2017-2018-2 20165318 实验四《Android程序设计》实验报告

    2017-2018-2 20165318 实验四<Android程序设计>实验报告 一.实验报告封面 课程:Java程序设计        班级:1653班        姓名:孙晓暄  ...

  7. BZOJ3211:花神游历各国(线段树)

    Description Input Output 每次x=1时,每行一个整数,表示这次旅行的开心度 Sample Input 4 1 100 5 5 5 1 1 2 2 1 2 1 1 2 2 2 3 ...

  8. JavaScript无阻塞加载具体方式

    将脚本放在底部.\还是放在head中,用以保证在js加载前,能加载出正常显示的页面.\<script>标签放在\前 成组脚本:由于每个\<script>标签下载时阻塞页面解析过 ...

  9. screen命令使用

    screen -S + name:创建一个名字叫做name的会话.在里面执行你想要执行的程序,再用Ctrl+a+d退出,让会话Detached,这样就能保证你的任务在后台一直运行,也不会随着终端的关闭 ...

  10. unset MAILCHECK

    文件/etc/profile尾部有: unset MAILCHECK 为了解决:每次登陆linux总是提示:you hava a new mail