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. HTML-class与id的区别及应用

    在样式表定义一个样式的时候,可以定义id也可以定义class. 1.在CSS文件里书写时,ID加前缀"#":CLASS用"." 如只能用id #nav { wi ...

  2. iphone照片查看器

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

  3. HDU 4360

    题意很好理解. 由于点是可以重复到达的,但可能每次经过路径的标志不一样,所以可以设每个点有四种状态"L”,'O','V','E'.然后按这些状态进行求最短路,当然是SPFA了. #inclu ...

  4. HDU 4526

    DP. 设状态dp[i][j]表示j辆车后还剩余i个人的花费,枚举一个车的座位k,加上剩下人数i,注意i+k不能超过n,就很容易dp了. #include <iostream> #incl ...

  5. 我要抓狂了。。又回到了几天不能A一道题的时候

    poj1556我不做了.你做做把...我已经要game over了

  6. apache zookeeper的安装

    original article:http://zookeeper.praveendeshmane.co.in/zookeeper/zookeeper-3-4-6-single-server-setu ...

  7. [Vue] Lazy Load a Route by using the Dynamic Import in Vue.js

    By default, vue-router doesn’t lazy load the routes unless you tell it to do it. Lazy loading of the ...

  8. AWS OpsWorks新增Amazon RDS支持

    AWS OpsWorks是一个应用管理服务. 你可以通过它把你的应用在一个 堆栈中定义成为不同层的集合.每一个堆栈提供了须要安装和配置的软件包信息,同一时候也能部署不论什么在OpsWorks层中定义的 ...

  9. hdu4430之枚举+二分

    Yukari's Birthday Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  10. Python 31 TCP协议 、socket套接字

    1.TCP协议 可靠传输,TCP数据包没有长度限制,理论上可以无限长,但是为了保证网络的效率,通常TCP数据包的长度不会超过IP数据包的长度,以确保单个TCP数据包不必再分割. (1)三次握手建链接( ...