PAT1110:Complete Binary Tree
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 思路
判断一棵二叉树是不是完全二叉树。 层次遍历二叉树,当遍历到"-"节点时(表示空节点),检查树的节点数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的更多相关文章
- [Swift]LeetCode919. 完全二叉树插入器 | Complete Binary Tree Inserter
A complete binary tree is a binary tree in which every level, except possibly the last, is completel ...
- A1110. Complete Binary Tree
Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...
- 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 ...
- PAT 甲级 1110 Complete Binary Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805359372255232 Given a tree, you are ...
- 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 ...
- [二叉树建树&完全二叉树判断] 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 ...
- 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 ...
- 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 ...
- 1110 Complete Binary Tree
1110 Complete Binary Tree (25)(25 分) Given a tree, you are supposed to tell if it is a complete bina ...
随机推荐
- C语言中 sscanf 的用法
名称: sscanf() - 从一个字符串中读进与指定格式相符的数据. 函数原型: Int sscanf( string str, string fmt, mixed var1, mixed var2 ...
- Android listView异步下载和convertView复用产生的错位问题
1:Item图片显示重复 这个显示重复是指当前行Item显示了之前某行Item的图片. 比如ListView滑动到第2行会异步加载某个图片,但是加载很慢,加载过程中ListView已经滑动到了第14行 ...
- 【matlab编程】matlab随机数函数
Matlab内部函数 a. 基本随机数 Matlab中有两个最基本生成随机数的函数. 1.rand() 生成(0,1)区间上均匀分布的随机变量.基本语法: rand([M,N,P ...]) 生成排列 ...
- (转载)SQL Server2008附加数据库之后显示为只读时解决方法
SQL Server2008附加数据库之后显示为只读时解决方法 啰嗦的话就不多说了,直入主题吧! 方案一: 碰到这中情况一般是使用的sa账户登录的,只要改为Windows身份验证,再附加数据库即可搞定 ...
- mysql 好文章
http://my.oschina.net/OpenSourceBO/blog/353464 http://www.oschina.net/p/cobar mysql集群 http://www.dew ...
- Makefile的obj-y 和 obj-m
目标定义是Kbuild Makefile的主要部分,也是核心部分.主要是定义了要编 译的文件,所有的选项,以及到哪些子目录去执行递归操作. 最简单的Kbuild makefile 只包含一行: 例子: ...
- Android群英传笔记——第五章:Android Scroll分析
Android群英传笔记--第五章:Android Scroll分析 滑动事件算是Android比较常用的效果了,而且滑动事件他本身也是有许多的知识点,今天,我们就一起来耍耍Scroll吧 一.滑动效 ...
- 数据包接收系列 — NAPI的原理和实现
本文主要内容:简单分析NAPI的原理和实现. 内核版本:2.6.37 Author:zhangskd @ csdn 概述 NAPI是linux新的网卡数据处理API,据说是由于找不到更好的名字,所以就 ...
- spring+mybaits多数据源使用
一.在利用spring管理mybatis时可以同时配置多个数据源,并且数据源可以随时切换,但在多线程中多数据源的事务需要一定的配置. 多数据源配置: <bean id="postgre ...
- 关于getchar函数缓冲区的问题
最近,看到有同学问我关于getchar()这个函数缓冲区的问题,结合我以前的学习,我将对getchar()进行一次总结,当然,这些都是别人已经提过的东西,我只是总结,接下来我们来看看. 首先,用get ...