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 ...
随机推荐
- VS2010安装Boost库
source URL: http://stackoverflow.com/questions/2629421/how-to-use-boost-in-visual-studio-2010 While ...
- mysql进阶(十二)常见错误汇总
原因:外键名不能重复
- 【Visual C++】游戏编程学习笔记之九:回合制游戏demo(剑侠客VS巡游天神)
本系列文章由@二货梦想家张程 所写,转载请注明出处. 作者:ZeeCoder 微博链接:http://weibo.com/zc463717263 我的邮箱:michealfloyd@126.com ...
- cocos2d-x 游戏开发之有限状态机(FSM) (一)
cocos2d-x 游戏开发之有限状态机(FSM) (一) 参考:http://blog.csdn.net/mgphuang/article/details/5845252<Cocos2d-x游 ...
- Linux常用命令(第二版) --压缩解压缩命令
压缩解压缩命令: ----------.gz---------- 1.压缩 gzip[GNU zip]: /bin/gzip 格式: gzip 选项 [文件] #压缩文件,压缩后扩展名为.gz,Lin ...
- OpenCV——老照片效果
// define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include < ...
- 使用“万能数据库查询分析器”的Windows 7、Windows 8、Windows 10的用户须知
与以前的Windows操作系统版本(包括WinXP/VISTA/Windows2000/WindowsNt/Win98)不同,Windows 7.Windows 8.Windows 10短日期采用的分 ...
- 【Android 应用开发】BluetoothServerSocket详解
一. BluetoorhServerSocket简介 1. 继承关系 public final class BluetoothServerSocket extends Object implement ...
- javac编译同一个包内的java文件
问题描述:包a.b.c里有d.java e.java f.java三个文件,其中d中包含main. 错误: 第一种:javac d.java 报错:里面用到其他类,找不到 第二种:javac *.ja ...
- Java + Selenium + TestNG + Maven
环境准备: 1. Java: Install Java jdk: Version: java 1.8 or aboveConfigure Java Environment Variables:Add ...