PAT_A1110#Complete Binary Tree
Source:
Description:
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 (≤) 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
YESand the index of the last node if the tree is a complete binary tree, orNOand 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
Keys:
- 完全二叉树(Complete Binary Tree)
Attention:
- atoi(s.c_str()); 字符串转化为int,atof 对应 double,atoll 对应 long long;
- 含非数字则截取前面的数字部分,首字符非数字则返回0
Code:
#include<cstdio>
#include<string>
#include<queue>
#include<iostream>
using namespace std;
const int M=;
int h[M]={};
struct node
{
int left,right;
}tree[M]; bool isComplete(int root, int &last)
{
queue<int> q;
q.push(root);
while(!q.empty())
{
root = q.front();
q.pop();
if(root != -)
{
last = root;
q.push(tree[root].left);
q.push(tree[root].right);
}
else
{
while(!q.empty())
{
if(q.front() != -)
return false;
q.pop();
}
}
}
return true;
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif int n,root,last;
scanf("%d", &n);
for(int i=; i<n; i++)
{
string l,r;
cin >> l >> r;
if(l == "-")
tree[i].left=-;
else{
tree[i].left = atoi(l.c_str());
h[tree[i].left]=;
}
if(r == "-")
tree[i].right=-;
else{
tree[i].right = atoi(r.c_str());
h[tree[i].right]=;
}
}
for(int i=; i<n; i++){
if(h[i]==){
root=i;
break;
}
}
if(isComplete(root, last))
printf("YES %d", last);
else
printf("NO %d", root); return ;
}
PAT_A1110#Complete Binary Tree的更多相关文章
- PAT1110:Complete Binary Tree
1110. Complete Binary Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- [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 ...
随机推荐
- iOS:制作左右侧滑(抽屉式)菜单
感谢控件作者:https://github.com/SocialObjects-Software/AMSlideMenu 首先上效果图: 这里我们使用AMSlideMenu来实现左右侧滑菜单的效果.控 ...
- 找出二叉查找树中指定结点的”下一个"结点(也即中序后继)
设计一个算法.找出二叉查找树中指定结点的"下一个"结点(也即中序后继).能够假定每一个结点都含有指向父结点的连接. watermark/2/text/aHR0cDovL2Jsb2c ...
- LA 3695 部分枚举
运用部分枚举的思想,很明显完全枚举点的思想是不可能的.改为枚举上下边界,当确定右边界j后,对左边界i,可以有点数为on[j]+on[i]+(leftu[j]-leftu[i])+leftd[j]-le ...
- Win10中如何把语言栏缩到系统托盘
Win10中如何把语言栏缩到系统托盘 原来语言栏是在系统托盘中的,右键点击,然后选择“显示语言栏”,就不能缩回去了: 后来在“控制面板\时钟.语言和区域\语言\高级设置”里面,有一个选项: “使用桌面 ...
- Oracle数据库导出导入
需求为将数据库A中的数据导出为*.dmp文件.然后将*.dmp文件导入到数据库B. 1.导出数据库A 在cmd窗体输入下面命令: 导出所有数据库 exp username/password@数 ...
- MySQL具体解释(15)-----------海量数据解说
第1章 引言 随着互联网应用的广泛普及,海量数据的存储和訪问成为了系统设计的瓶颈问题. 对于一个大型的互联网应用.每天几十亿的PV无疑对数据库造成了相当高的负载.对于系统的稳定性和扩展性造成了极大的 ...
- JavaScript探秘:强大的原型和原型链
// foo 变量是上例中的 for(var i in foo) { if (foo.hasOwnProperty(i)) { console.log(i); } } JavaScript 不包括传统 ...
- cocos2d-x 3.2 之 2048 —— 第二篇
***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...
- Framebuffer 机制【转】
本文转载自:http://blog.csdn.net/paul_liao/article/details/7706477 Framebuffer Framebuffer是Linux系统为显示设备提供的 ...
- 杂项-Java:标签库
ylbtech-杂项-Java:标签库 1.返回顶部 1. JSP标签库,也称自定义标签库,可看成是一种通过JavaBean生成基于XML的脚本的方法.从概念上讲,标签就是很简单而且可重用的代码结构. ...