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 ( ...
随机推荐
- HTTPS_SSL apache认证、配置的、步骤以及原理说明
一 .1.单向认证,就是传输的数据加密过了,但是不会校验客户端的来源 2.双向认证,如果客户端浏览器没有导入客户端证书,是访问不了web系统的,找不到地址,想要用系统的人没有证书就访问不了系统HTTP ...
- 静态性能测试-hc课堂笔记
UI自动化,需要掌握html相关知识 w3c网站. 会了性能测试就会了接口自动化. 静态扫描:降低40-50% findbugs,隐含的bug checkstyle,风格规范 域名解析: 输入网址-D ...
- C++之强制类型转化
在C++语言中新增了四个关键字static_cast.const_cast.reinterpret_cast和dynamic_cast.这四个关键字都是用于强制类型转换的.我们逐一来介绍这四个关键字. ...
- vi 替换
在vi编辑器中,能够利用 :s命令能够实现字符串的替换.详细的使用方法例如以下: 1.:s/str1/str2/ 用字符串 str2 替换行中首次出现的字符串str1: 2.:s/str1/str2/ ...
- WFP page navigator control
WPF navigator UI: <Grid x:Class="WpfApplication2.PagerNav" xmlns="http://schemas.m ...
- 20155227《网络对抗》Exp4 恶意代码分析
20155227<网络对抗>Exp4 恶意代码分析 实践目标 1.是监控你自己系统的运行状态,看有没有可疑的程序在运行. 2.是分析一个恶意软件,就分析Exp2或Exp3中生成后门软件:分 ...
- 洛咕 P2336 [SCOI2012]喵星球上的点名
洛咕 P2336 [SCOI2012]喵星球上的点名 先求出SA和height,一个点名串对应的就是一段区间,还有很多个点,就转化成了 有很多个区间,很多个点集,对每个区间计算和多少个点集有交,对每个 ...
- xml中该使用属性还是元素
XML 中没有规定哪些必须放在属性或者子元素,因此使用哪种方式都是可以实现的.这取决于个人的经验和喜好.在可以使用元素也可以使用属性的两选一的情况下,个人更倾向于使用子元素.主要理由如下: 1. 属性 ...
- Django中的cookie和session
前言 HTTP协议 是短连接.且状态的,所以在客户端向服务端发起请求后,服务端在响应头 加入cokie响应给浏览器,以此记录客户端状态: cook是来自服务端,保存在浏览器的键值对,主要应用于用户登录 ...
- 写个发邮件的功能php的(全代码)
---恢复内容开始--- 正好做了个项目,需要在线留言,一般在线留言发邮件是很常见的方式,一开始从网上搜了很久都没有很全的,也有全一点的,但是也不能用,运行不成功,下面给大家分享一下运行成功了的全部代 ...