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 ( ...
随机推荐
- Docker技术入门与实战 第二版-学习笔记-2-镜像构建
3.利用 commit 理解镜像构成 在之前的例子中,我们所使用的都是来自于 Docker Hub 的镜像. 直接使用这些镜像是可以满足一定的需求,而当这些镜像无法直接满足需求时,我们就需要定制这些镜 ...
- WorldWind源码剖析系列:可渲染对象类RenderableObject
RenderableObject是WorldWind中所有需要渲染的对象的父类,继承了接口IRenderable和Icomparable.其派生类体系如下所示.RenderableObject的成员如 ...
- stm32 boot0 boot1的启动方式
STM32三种启动模式对应的存储介质均是芯片内置的,它们是: 1)用户闪存 = 芯片内置的Flash. 2)SRAM = 芯片内置的RAM区,就是内存啦. 3)系统存储器 = 芯片内部一块特定的区域, ...
- Python2.7-NumPy
NumPy 提供了两种基本对象ndarray(N-dimensional array object)和 ufunc(universal function object)ndarray(下文统一称之为数 ...
- 修复在“Android 在ScrollView中嵌入ViewPage后ViewPage不能很好的工作的问题解决”这篇博客中MyScrollView出现滑动一会就不会上下滑动的问题
在“Android 在ScrollView中嵌入ViewPage后ViewPage不能很好的工作的问题解决”,这篇博客中的大部分问题已经解决了. 唯一遗憾的是,ViewPage随人能够工作了,但是My ...
- 笔记:UITextView内容垂直居中方法
- (void)contentSizeToFit { //先判断一下有没有文字(没文字就没必要设置居中了) ) { //textView的contentSize属性 CGSize contentSiz ...
- BAT for 循环
@echo off echo.Current User is '%USERNAME%'echo.This script must run with administrative privileges ...
- 20155238 实验四 Android程序设计
Android 安装Android Studio 按照教程依次完成安装步骤.安装所存的相应文件夹必须纯英文,不能出现特殊字符. 32位系统和64位系统是同一个安装文件.启动程序中32位与64位都有.根 ...
- SQL Server 启动时发生错误1069:由于登录失败而无法启动
解决方法: (1). 我的电脑--控制面板--管理工具--服务--右键MSSQLSERVER--属性--登陆--登陆身份--选择"本地系统帐户". (2). 我的电脑- ...
- POJ 2388&&2299
排序(水题)专题,毕竟如果只排序不进行任何操作都是极其简单的. 事实上,排序算法十分常用,在各类高级的算法中往往扮演着一个辅助的部分. 它看上去很普通,但实际的作用却很大.许多算法在失去排序后将会无法 ...