Source:

PAT A1110 Complete Binary Tree (25 分)

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

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的更多相关文章

  1. PAT1110:Complete Binary Tree

    1110. Complete Binary Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

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

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

  3. A1110. Complete Binary Tree

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

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

  5. PAT 甲级 1110 Complete Binary Tree

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

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

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

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

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

随机推荐

  1. [luogu2209][USACO13]燃油经济性Fuel Economy_贪心

    燃油经济性Fuel Economy 题目大意:FJ想要去旅行.他的车总容量为G,每行驶一个单位就消耗一个单位的油.FJ要行驶D个单位的距离.期间存在n个加油站,每个加油站有一个价格,表示在这个燃油站买 ...

  2. AngularJS:一行JS代码实现控件验证效果

    如上图所示,我们需要实现如下这些验证功能: 控件都是必输控件 都需要控制最大长度 第一次打开页面,控件不能显示为错误状态 输入内容再清空后,必输控件需要显示为错误状态 只有所有输入合法后,发布按钮才能 ...

  3. 豆瓣 jsonp 请求数据 并分页

    豆瓣分页 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4 ...

  4. HDU 4543

    这道题感觉很坑..不过,注意一些小问题. 参考http://www.cnblogs.com/Lattexiaoyu/archive/2013/03/31/2992553.html改进了原来自己的复杂度 ...

  5. oracle加入联合主键

    alter table tablename add constraint unionkeyname primary key (column1,column2); 上面语句中: tablename为要加 ...

  6. Microsoft ASP.NET SignalR

    SignalR类似与JavaScript实时框架,如Socket.IO.SignalR能够完成客户端向服务器的异步通信,并同时支持服务器向浏览器客户端推送事件.SignalR的连接通过日益流行的Web ...

  7. Spring Boot 特性 —— SpringApplication

    转自:https://blog.csdn.net/cqdz_dj/article/details/52910317

  8. Struts2标签库常用标签

    转自:https://blog.csdn.net/q547550831/article/details/53326042

  9. centos的vsftp修改上传下载速度

    比如你想限制本地用户的上传速度和下载速度为1MB/s,则在vsftpd.conf中添加以下内容:local_max_rate = 1048576 ※ 默认单位是Byte/s

  10. tpshop编辑框中上传图片过大变模糊

    tpshop编辑框中上传图片过大变模糊 图片超过2500的高就会变模糊 设置最大的高度修改一下