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. Color the ball 线段树 区间更新但点查询

    #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #inclu ...

  2. HDU 3167 KMP

    很久之前做的一题,忽然想起来,依然觉得思路巧妙. //这道题,确实是一道好题.但如何应用KMP,确实大大超出了意料中. //这道题匹配的是某元素在子串中的名次,也就是在子串中排第几小.我想了整整一天, ...

  3. 2014百度之星第二题Disk Schedule(双调欧几里得旅行商问题+DP)

    Disk Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  4. IE6、IE7的兼容问题

    通常,网页的兼容问题,就是IE6\IE7的问题.表现为错位.换行,不支持CSS3等. 而其中,错位.换行,原因往往在于没有指明元素的width.height. 一般银瓦不告诉他.

  5. 通过命令行升级git for windows

    git update-git-for-windows 配置了正确的代理,就可以通过命令行直接升级.最好是可以访问谷歌的代理,否则国内的网络通过命令行升级,下载到一半,就会失败.

  6. Codeforces--630H--Benches(组合数)

    H - Benches Crawling in process... Crawling failed Time Limit:500MS     Memory Limit:65536KB     64b ...

  7. bzoj4373 算术天才⑨与等差数列——线段树+set

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4373 一个区间有以 k 为公差的数列,有3个条件: 1.区间 mx - mn = (r-l) ...

  8. springMVC返回数据的四种方式

    转自:https://blog.csdn.net/itcats_cn/article/details/82119673 springMVC返回数据的四种方式:第一种,通过request.setAttr ...

  9. sublime如何汉化

    1.将sublime安装文件夹里面的defavlut.sublime-package这个文件zip解压. 2.然后查找到sublime-menu文件. 3.打开文件将json里面的caption里面的 ...

  10. hdu3652B-number(数位dp)

    B-number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...