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 -- PDO事务处理

    事务处理流程 开启事务 PDO::beginTransaction 事务操作 所有的实务操作就是增删改 事务提交 PDO::commit:成功后提交数据 PDO::rollback:失败后回滚数据 & ...

  2. java中不用BigInteger实现超大整数的乘法操作

    昨天看到一个题目:计算1234!,不能用BigInteger类 众所周知阶乘的数据会非常大,经常使用的int和long型根本不够用.一般想到的仅仅有BigInteger类,可是题目中明白说了不能用,所 ...

  3. oracle中恢复删除的表

    1.表恢复,如果在删除表的同时删除的数据,那么表恢复也能恢复当时删除时的数据 -----查询删除的表 select * from recyclebin order by droptime desc - ...

  4. RabbitMQ OS X下安装及常用命令-1

            RabbitMQ的主页在http://www.rabbitmq.com/ . 1. 安装Erlang RabbitMQ是用Erlang编写的,所以需要先安装Erlang,如果有的话跳过 ...

  5. 1 week110的zookeeper的安装 + zookeeper提供少量数据的存储

    随时查看,zookeeper企业里公认的最新文档版本!       https://archive.apache.org/dist/    下面是在weekend110上的zookeeper的安装 在 ...

  6. WPF XAML 特殊字符(小于号、大于号、引号、&符号)

    XAML 受限于 XML 规则.例如, XML 特别关注一些特殊字符,如  & < > 如果试图使用这些字符设置一个元素内容,将会遇到许多麻烦,因为 XAML 解析器认为您正在做其 ...

  7. js控制radio选中

    经常会遇到js控制radio选中和切换的问题 之前一直使用的是checked属性来完成的 但是现在发现这个属性有个大问题 今天就是用js给选中radio的赋值,使用的$().attr("ch ...

  8. STL 源代码剖析 算法 stl_algo.h -- inplace_merge

    本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie inplace_merge(应用于有序区间) ----------------------- ...

  9. 类似hibernate实现sql增删改错

    Util package utils; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Ha ...

  10. 按批次处理list数据 (list按条数取)

    按批次处理list数据的两种方法 主要应用于list存储数据过多,不能使list整体进行其余操作 Java | 复制 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...