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的更多相关文章

  1. PAT甲级——1110 Complete Binary Tree (完全二叉树)

    此文章同步发布在CSDN上:https://blog.csdn.net/weixin_44385565/article/details/90317830   1110 Complete Binary ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  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. 1110 Complete Binary Tree

    1110 Complete Binary Tree (25)(25 分) Given a tree, you are supposed to tell if it is a complete bina ...

  9. PAT 甲级 1064 Complete Binary Search Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805407749357568 A Binary Search Tree ( ...

随机推荐

  1. HDU3853:LOOPS

    题意:迷宫是一个R*C的布局,每个格子中给出停留在原地,往右走一个,往下走一格的概率,起点在(1,1),终点在(R,C),每走一格消耗两点能量,求出最后所需要的能量期望   #include<i ...

  2. centos6.4安装 zabbix agent

    1.防火墙设置 允许zabbix-agent的10050端口通过  iptables -A INPUT -p tcp --dport 10050 -j ACCEPT  2.安装zabbix agent ...

  3. 第4章 初识STM32

    第4章     初识STM32 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/firege ...

  4. 怎样让oracle实验本在不做实验时性能提升——win7下举例

    怎样让oracle实验本在不做实验时性能提升--win7下举例 型号:ThinkPad E431 系统:WIN7 实验使用的笔记本不使用数据库时.建议将oracle关闭,使其释放占用的资源. orac ...

  5. 20155238 2016-2017-2《Java程序设计》课程总结

    每周作业链接汇总(按顺序) 预备作业1 预备作业2 预备作业3 第一周作业 第二周作业 第三周作业 第四周作业 第五周作业 第六周作业 第七周作业 第八周作业 第九周作业 第十周作业 自认为写得最好一 ...

  6. 20155339 Exp8 Web基础

    Exp8 Web基础 基础问题回答 (1)什么是表单 表单在网页中主要负责数据采集功能. 一个表单有三个基本组成部分: 表单标签,这里面包含了处理表单数据所用CGI程序的URL以及数据提交到服务器的方 ...

  7. [arm学习]makefile学习总结

    makefile不仅仅是一个命令的集合体,其中有一些规则是需要理解掌握的. 首先,了解makefile的规则: //-----------格式---------- 目标 : 依赖1,依赖2 (TAP键 ...

  8. 微信小程序 Echarts 异步数据更新

    微信小程序 Echarts 异步数据更新的练习,被坑了很多次,特作记录. 作者:罗兵 地址:https://www.cnblogs.com/hhh5460/p/9989805.html 0.效果图   ...

  9. python 回溯法 子集树模板 系列 —— 19、野人与传教士问题

    问题 在河的左岸有N个传教士.N个野人和一条船,传教士们想用这条船把所有人都运过河去,但有以下条件限制: (1)修道士和野人都会划船,但船每次最多只能运M个人: (2)在任何岸边以及船上,野人数目都不 ...

  10. 使用pandas,7行代码实现朴素贝叶斯

    作者:hhh5460 大抵分成两类 一.离散的.标签化的数据 原文没有使用pandas,我使用pandas重新实现了朴素贝叶斯算法,看起来非常简洁.清爽. import pandas as pd '' ...