题意:判断一个节点为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. Nginx的配置使用

    因为做了一个聚合支付的服务应用,对于交易系统来说,并发要求比较高,所以需要使用负载均衡来缓解并发的要求,自行开发又太费时费力,Nginx查了些资料基本满足需求,故对其做了下研究,记录下防止遗忘. 一. ...

  2. 【CSS】Sass理解

    原文在 https://github.com/zhongxia245/blog , 欢迎 star! Sass理解 时间:2016-09-24 22:56:12 作者:zhongxia 这里就不讲解S ...

  3. 根据进程ID查找运行程序目录

    查看进程ID [root@hadoop03 openresty]# netstat -nltp 进入/proc目录查找相应进程ID目录并进入此目录 [root@hadoop03 usr]# cd /p ...

  4. windows 2012 抓明文密码方法

    windows 2012 抓明文密码方法 默认配置是抓不到明文密码了,神器mimikatz显示Password为null Authentication Id : 0 ; 121279 (0000000 ...

  5. 未能找到 CodeDom 提供程序类型“Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider,

    未能找到 CodeDom 提供程序类型“Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft ...

  6. 【Ansible 文档】【译文】入门教程

    http://docs.ansible.com/ansible/latest/intro_getting_started.html Foreword 前言 到这里,你应该已经安装了Ansible,是时 ...

  7. php无限分类 下拉框

    无限分类 下拉框优势:填写参数少,只需要指定一个循环节点($parnent_id),就可以循环所有下级分类.循环输出结构很有特色,比较符合我的口味.补充: $parent_id才是上下级关联的节点,i ...

  8. git回答整理

    1.git常用命令 首先明确:git有工作区.暂存区.版本库,工作区是电脑里能看到的目录 创建仓库: git init newrepo,使用我们指定目录作为Git仓库(初始化后,会在newrepo目录 ...

  9. scapy学习笔记(4)简单的sniffing 嗅探

    转载请注明:@小五义:http://www.cnblogs/xiaowuyi 利用sniff命令进行简单的嗅探,可以抓到一些简单的包.当不指定接口时,将对每一个接口进行嗅探,当指定接口时,仅对该接口进 ...

  10. C++STL之Vector的应用

    这是我第一次写博客,请多指教! vector是一种向量容器,说白了就是可以改变大小的数组. vector是一个模板类,如果直接这样会报错: vector a; //报错,因为要指定模板. 需要像这样: ...