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

题目大意:给出一个二叉树,判断是否是完全二叉树。

//这个题真的学到了不少东西。我的AC:

参照了:https://blog.csdn.net/hyf20144055065/article/details/51970789

#include <iostream>
#include <algorithm>
#include<cstdio>
#include<stdio.h>
#include <queue>
#include<cmath>
#include <vector>
using namespace std;
struct Node{
int left=-,right=-;
}node[];
int isr[];
int root=-;
int ct=,n,last=;
void level(int r){//进行层次遍历。我是选择在入队的时候+,但是这样是比较复杂的,我应该在出队的时候+。
queue<int> qu;
qu.push(r);
//ct++;
while(!qu.empty()){
//if(ct==n)last=qu.back();
int top=qu.front();qu.pop();
if(top==-)break;
last=top;//每次last都赋值为当前。
ct++;
// if(node[top].left==-1)break;
// else {
// qu.push(node[top].left);
// ct++;
// }
qu.push(node[top].left);//其实这里-1完全可以push进去,因为下一次再次弹出时会进行判断的。
qu.push(node[top].right);
// if(node[top].left!=-1){
// qu.push(node[top].right);
// ct++;
// }
}
} int main()
{
cin>>n;
string l,r;
for(int i=;i<n;i++){
cin>>l>>r;
if(l!="-"){
node[i].left=atoi(l.c_str());//注意这里的转换
//cout<<node[i].left<<'\n';
isr[node[i].left]=;
}
if(r!="-"){
node[i].right=atoi(r.c_str());
isr[node[i].right]=;
}
}
for(int i=;i<n;i++){
if(isr[i]==){
root=i;break;
}
}
level(root);
if(ct==n)
cout<<"YES "<<last;
else
cout<<"NO "<<root;
return ;
}

1.利用完全二叉树的性质来判断。

2。使用了层次遍历,但是我的层次遍历思路是不正确的。

3.我的思路:只要有左子节点就push进去,然后计数+1,是在入队的时候计算数量。只要左子节点为-1,那么就break掉。

4.正确思路:当节点左右为空-1时,可以push进去,在whlle循环中会进行判断,如果是-1,那么就break了,是在出队的时候进行+1操作,这样最后就可以了

5.在我原来的那种方法中,2和6测试点过不去,对于6测试点:

使用了

1

- -

测试数据进行了更正

2测试点是对于0的检测。

正确输出应该是YES  0

PAT 1110 Complete Binary Tree[比较]的更多相关文章

  1. PAT 1110 Complete Binary Tree[判断完全二叉树]

    1110 Complete Binary Tree(25 分) Given a tree, you are supposed to tell if it is a complete binary tr ...

  2. PAT 1110 Complete Binary Tree

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

  3. PAT甲级——1110 Complete Binary Tree (完全二叉树)

    此文章同步发布在CSDN上:https://blog.csdn.net/weixin_44385565/article/details/90317830   1110 Complete Binary ...

  4. 1110 Complete Binary Tree (25 分)

    1110 Complete Binary Tree (25 分) Given a tree, you are supposed to tell if it is a complete binary t ...

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

  6. 1110 Complete Binary Tree

    1110 Complete Binary Tree (25)(25 分) Given a tree, you are supposed to tell if it is a complete bina ...

  7. PAT 甲级 1110 Complete Binary Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805359372255232 Given a tree, you are ...

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

  9. PAT A1110 Complete Binary Tree (25 分)——完全二叉树,字符串转数字

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

随机推荐

  1. PHP 安全三板斧:过滤、验证和转义之转义篇 & Blade模板引擎避免XSS攻击原理探究

    PHP 转义 实现 把输出渲染成网页或API响应时,一定要转义输出,这也是一种防护措施,能避免渲染恶意代码,造成XSS攻击,还能防止应用的用户无意中执行恶意代码. 我们可以使用前面提到的 htmlen ...

  2. eclipse不能自动编译生成class文件的解决办法

    最近在项目项目开发过程中遇到eclipse不能自动编译生成class文件,当时很纳闷,每次修改代码后运行都是修改前的效果,没辙了,只好反编译原来的class文件,结果发现,class文件里并没有看到修 ...

  3. TensorFlow基础笔记(3) cifar10 分类学习

    TensorFlow基础笔记(3) cifar10 分类学习 CIFAR-10 is a common benchmark in machine learning for image recognit ...

  4. Spring Boot可以离开Spring Cloud独立使用开发项目,但是Spring Cloud离不开Spring Boot,属于依赖的关系。

    Spring Boot 是 Spring 的一套快速配置脚手架,可以基于Spring Boot 快速开发单个微服务,Spring Cloud是一个基于Spring Boot实现的云应用开发工具:Spr ...

  5. 注解-->Spring配置

    有必要对JDK 5.0新增的注解(Annotation)技术进行简单的学习,因为Spring 支持@AspectJ,而@AspectJ本身就是基于JDK 5.0的注解技术.所以学习JDK 5.0的注解 ...

  6. v8随心记

    因为node原因,研究v8已经有段时间了,但是一直也没有抽空写点什么,现在想想有好多当时清晰的东西又模糊了.哎,伤心的很啊.但是一时又想不起什么章法,所以只能随手胡乱写了. 下载.编译: https: ...

  7. 工作流JBPM_day01:1-说明_MyProcessDesigner_流程设计器

    工作流JBPM_day01:1-说明 先只做请假功能,怎么做? (请假可以和考勤整合到一起) 1,银行(拿号---叫号---办理) 2,餐馆(点菜---上菜---结账) 3,网购(下订单--配送--收 ...

  8. 权限模块_整体方案说明_设计实体&映射实体_实现初始化权限数据的功能

    权限模块_整体方案说明 要点说明 权限就是控制功能的使用(功能对应着URL). 对功能的控制就是对URL的访问控制. 在我们的程序中,一个功能对应一个或两个URL: 1,例如列表或删除功能,只对应一个 ...

  9. Spring.NET学习笔记——目录(原)

    目录 前言 Spring.NET学习笔记——前言 第一阶段:控制反转与依赖注入IoC&DI Spring.NET学习笔记1——控制反转(基础篇) Level 200 Spring.NET学习笔 ...

  10. 用httpclient做压力测试时Too many open files的解决办法

    在工作过程中,用httpclient去压测一个web api,发现压一小段时间就出现了Too many open files.实际上,HttpClient建立Socket时 ,post.release ...