PAT 甲级 1110 Complete Binary Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805359372255232
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
代码:
#include <bits/stdc++.h>
using namespace std; int N;
int vis[110];
int ans = -1, temp; struct Node{
int l;
int r;
}node[110]; int StringtoInt(string s) {
int len = s.length();
int sum = 0;
for(int i = 0; i < len; i ++) {
sum = sum * 10 + (s[i] - '0');
}
return sum;
} void dfs(int st, int step) {
if(step > ans) {
ans = step;
temp = st;
} if(node[st].l != -1) dfs(node[st].l, step * 2);
if(node[st].r != -1) dfs(node[st].r, step * 2 + 1);
} int main() {
scanf("%d", &N);
memset(vis, 0, sizeof(vis));
for(int i = 0; i < N; i ++) {
string s1, s2;
cin >> s1 >> s2;
if(s1 == "-") {
node[i].l = -1;
} else if(s1 != "-"){
node[i].l = StringtoInt(s1);
//cout << node[i].l << endl;
vis[node[i].l] = 1;
} if(s2 == "-") {
node[i].r = -1;
} else if(s2 != "-") {
node[i].r = StringtoInt(s2);
vis[node[i].r] = 1;
//cout << node[i].r << endl;
}
} int root = 0;
while(vis[root]) root ++;
dfs(root, 1); if(ans == N)
printf("YES %d\n", temp);
else printf("NO %d\n", root);
return 0;
}
判断是不是完全二叉树要判断这个树是不是满的 看是不是最大的节点数等于一共的节点数目 dfs 求最大的节点下标
PAT 甲级 1110 Complete Binary Tree的更多相关文章
- PAT甲级——1110 Complete Binary Tree (完全二叉树)
此文章同步发布在CSDN上:https://blog.csdn.net/weixin_44385565/article/details/90317830 1110 Complete Binary ...
- 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 Advanced 1110 Complete Binary Tree (25) [完全⼆叉树]
题目 Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each ...
- 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 (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 ...
- 1110 Complete Binary Tree
1110 Complete Binary Tree (25)(25 分) Given a tree, you are supposed to tell if it is a complete bina ...
- PAT 甲级 1064 Complete Binary Search Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805407749357568 A Binary Search Tree ( ...
随机推荐
- 如何取消idea中的本地源码关联
- 【转】Android Service创建USB HOST通信
之前做了一个关于Android USB通信的Case,通过Android的USB总线给Zigbee供电,和板载的Zigbee(基于Zigbee的自组网)进行通信.要使用Android的USB Host ...
- Maven单独构建多模块项目中的单个模块
Maven单独构建多模块项目中的单个模块 说明: 1.可能存在的场景,多模块项目没有互相引用,那么此时可以单独构建单个项目,指定到子模块的pom.xml文件即可完成编译. 2.如果多模块项目各自都 ...
- MP实战系列(十)之SpringMVC集成SpringFox+Swagger2
该示例基于之前的实战系列,如果公司框架是使用JDK7以上及其Spring+MyBatis+SpringMVC/Spring+MyBatis Plus+SpringMVC可直接参考该实例. 不过建议最好 ...
- day 21 今日学习内容
今日没有学习新的内容,可能今天就是对于前一段时间学习的总结,今天做了一个相对之前作业更加完善的ATM+购物车,在今天的学习里,我对于编程有了新的见解,编程并非一味的for..if...for...更多 ...
- 第39章 ETH—Lwip以太网通信
第39章 ETH—Lwip以太网通信 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/ ...
- POJ3278&&1426&&3126&&3087&&3414
接上次的,标签是BFS专题 其实无论是Deepth还是Breadth都是Search 3278 又是撸过的题目 1426 找一个十进制数(我刚开始看样例以为是二进制数),使得它每一位上都是1或0,且是 ...
- STM32烧录的常用方式
stm32烧录常用的方式一般为ST-LINK(或者J-tag)下载仿真和ISP下载 一.仿真器下载 仿真器分为J-TAG和SWD仿真,SWD仿真只需要4根线(VCC.GND.CLK.DATA)就可以了 ...
- [BZOJ2687]交与并[决策单调性]
题意 给定 \(n\) 个区间,我们定义区间集合 \(S(|S|>1)\) 的权值为 区间交 \(\times\) 区间并,找出权值最大的区间集合. \(n\le 10^6\) 分析 首先排除区 ...
- 2017qq红包雨最强攻略
这个只支持苹果手机,而且要有苹果电脑,只有苹果手机是不行的. QQ红包规则:只要你到达指定的位置,就可以领取附近的红包,一般也就几毛,还有几分的,当然也不排除有更高的,只不过我是没遇到... 那么既然 ...