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. 流式套接字:基于TCP协议的Socket网络编程(案例2)

    案例:在案例1的基础上实现一个服务器对应多个客户端(多线程),且获得每个客户端的IP. 线程代码: package com.yh.mySocket; import java.io.BufferedRe ...

  2. oracle中over函数

    1.oracle中按一个字段分组排序后取第一条数据. SELECT * FROM (SELECT ROW_NUMBER() OVER(PARTITION BY 分组字段 ORDER BY 排序字符 D ...

  3. OpenCV——HOG特征检测

    API: HOGDescriptor(Size _winSize, ---:窗口大小,即检测的范围大小,前面的64*128 Size _blockSize,--- 前面的2*2的cell,即cell的 ...

  4. OpenCV——视频操作基础

    读入视频: VideoCapture 类 //方法一 VideoCapture capture; capture.open("test.avi"); //方法二 VideoCapt ...

  5. C# 含转义符的字符串处理

    如果一个字符串中含有特殊字符,比如"号,如何将一个含有引号"的字符串赋值给一个变量. string a = @"sfsfsf"""; str ...

  6. 同步IO和异步IO的区别

    首先一个IO操作其实分成了两个步骤:发起IO请求和实际的IO操作,同步IO和异步IO的区别就在于第二个步骤是否阻塞,如果实际的IO读写阻塞请求进程,那么就是同步IO,因此阻塞IO.非阻塞IO.IO服用 ...

  7. cleanCode[1]:有意义的命名

    为什么要有意义的命名: 我们都曾经说过有朝一日再回头清理那些糟糕的代码,然而最终总是弃之不顾.稍后等于永不,我们需要立即行动,写优雅的代码. 写代码的过程中,读占的比例很大,所以首先要让代码易读. 有 ...

  8. 20155207王雪纯《网络对抗》Exp4 恶意代码分析

    20155207 <网络对抗> 恶意代码分析 学习总结 实践目标 1.是监控你自己系统的运行状态,看有没有可疑的程序在运行. 2.是分析一个恶意软件,就分析Exp2或Exp3中生成后门软件 ...

  9. 20155330 《网络对抗》 Exp7 网络欺诈防范

    20155330 <网络对抗> Exp7 网络欺诈防范 基础问题回答 通常在什么场景下容易受到DNS spoof攻击 连接局域网的时,特别是在商场或是别的公众较多的场合连接的免费WIFI. ...

  10. Octocat,看着喜欢就都下载下来了

    看见github的octocat很喜欢,就用c#写了个程序统统download了,附上一个比较高效的下载程序,以及文末的图片压缩包. 用到了Jumony解析网页. HttpClient client ...