1110. Complete Binary Tree (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

思路
判断一棵二叉树是不是完全二叉树。 层次遍历二叉树,当遍历到"-"节点时(表示空节点),检查树的节点数N和遍历次数cnt是否相等,相等yes,不想等no。
注意:输入用string而不用char,因为char不能表示两位数以上的数字。之前没注意导致代码只有部分ac。 代码

 

#include<iostream>
#include<vector>
#include<queue>
using namespace std; class Node
{
public:
int left;
int right;
};
int main()
{
int N;
while(cin >> N)
{
vector<Node> nodes(N);
vector<bool> isroot(N,true);
//Build tree
for(int i = 0;i < N;i++)
{
string left,right;
cin >> left >> right;
if(left == "-")
nodes[i].left = -1;
else
{
nodes[i].left = stoi(left);
isroot[nodes[i].left] = false;
}
if(right == "-")
nodes[i].right = -1;
else
{
nodes[i].right = stoi(right);
isroot[nodes[i].right] = false;
}
}
//Find root
int root = -1;
for(int i = 0;i < isroot.size();i++)
{
if(isroot[i])
{
root = i;
break;
}
}
//BFS
queue<int> q;
q.push(root);
int cnt = 0,lastindex = -1;
while(!q.empty())
{
int tmp = q.front();
q.pop();
if(tmp == -1)
{
break;
}
cnt++;
lastindex = tmp;
q.push(nodes[tmp].left);
q.push(nodes[tmp].right);
}
//Output
if(cnt == N)
cout << "YES" << " " << lastindex <<endl;
else
cout << "NO" << " " << root << endl;
}
}

  

PAT1110:Complete Binary Tree的更多相关文章

  1. [Swift]LeetCode919. 完全二叉树插入器 | Complete Binary Tree Inserter

    A complete binary tree is a binary tree in which every level, except possibly the last, is completel ...

  2. A1110. Complete Binary Tree

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

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

  4. PAT 甲级 1110 Complete Binary Tree

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

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

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

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

  8. PAT 1110 Complete Binary Tree[比较]

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

  9. 1110 Complete Binary Tree

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

随机推荐

  1. 如何反编译APK?

    1.概述 一些商业的app都包含很多精美的图片还有一些比较好的配置文件,以前某师兄就说过apk把后缀改为zip,然后解压一下就可以获得很多图片资源,但是这时候你打开一下解压出来的xml资源全是乱码.通 ...

  2. 64位ubuntu14.04配置adb后提示没有那个文件或目录

    1.配置完adb环境变量后在终端输入adb: ameyume@ameyume-HP-450-Notebook-PC:~$ adb /home/ameyume/adt-bundle-linux-x86_ ...

  3. Android服务器——使用TomCat实现软件的版本检测,升级,以及下载更新进度!

    Android服务器--使用TomCat实现软件的版本检测,升级,以及下载更新进度! 算下来,TomCat服务器已经写了很长一段时间了,一直说拿他来搞点事 情,也一直没做,今天刚好有空,交流群还有人请 ...

  4. Android ROM开发(三)——精简官方ROM并且内置ROOT权限,开启Romer之路

    Android ROM开发(三)--精简官方ROM并且内置ROOT权限,开启Romer之路 相信ROM的相关信息大家通过前几篇的学习都是有所了解了,这里就不在一一提示了,这里我们下载一个官方包,我们还 ...

  5. source insight 中tab键的设置

    转:http://xinzero.com/source-insight-code-alignment-ended.html source insight代码对齐Tab键终极版 以前也写过一个sourc ...

  6. 和菜鸟一起学linux总线驱动之i2c死锁问题

    不知不觉中已经有好几个月没有写点东西了,懒了就是懒了,说是忙着想把产品做得更好,都是借口,每天花一点时间来写点东西确实很不错,自己也坚持了很久很久,只不过今年以来,发现提高不是很大,能写的东西好少好少 ...

  7. 开源项目AndroidReview学习小结(1)

    多看多学涨姿势 最近学习了一个开源项目,感觉收获颇多,这里做下简要的记录,首先感谢作者的开源.先看个大概图 感觉框架非常简单,界面也很一般,不过底层的处理的一些处理还是有很多可圈可点之处,代码的处理一 ...

  8. UML类图简介

    概述 设计模式中常常使用UML来表示类与类,类与接口之间的关系,UML类图是设计模式入门必备的技能,感觉各种关系比较多,这里做一下总结. 类与接口的表示 类与接口通常是一个矩形框表示,一般分为3层,第 ...

  9. OS X10.10下HomeBrew的安装提示

    apple@kissAir: c_src$ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/m ...

  10. form表单提交转为可被 getModel(PROJECT.class ,null);接收

    var form = new mini.Form("#editForm"+id); form.validate();if (!form.isValid()) { alert('信息 ...