PAT 1110 Complete Binary Tree[判断完全二叉树]
1110 Complete Binary Tree(25 分)
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 (≤20) 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 NOand 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
题目大意:给出一棵树二叉树,判断是否是完全二叉树,如果是那么输出最后一个节点;如果不是输出根节点。
//第一次见完全二叉树的题目,想起了完全二叉树的性质,存储树的话,就用结构体数组,下标表示当前节点号;首先求出树的高度根据logn,看是否余数为0,判断是否+1;那么前n-1层的节点要是满的,并且再通过只有一个左子节点或者右子节点的树只有一个,那么来判断是否是完全二叉树;并且结构体里有一个属性是father默认为-1。感觉好复杂,就没有用代码实现。
代码来自:https://www.liuchuo.net/archives/2158
#include <iostream>
#include <queue>
#include <vector>
#include <string>
using namespace std;
struct TREE {
int left, right;
};
int main() {
int n, root = ;
scanf("%d", &n);
vector<TREE> tree(n);
vector<int> book(n);
for(int i = ; i < n; i++) {
string l, r;
cin >> l >> r;//使用字符串读取,也必须使用字符串,
if(l == "-") {
tree[i].left = -;//如果左右为空的话,则标记为-1.
} else {
tree[i].left = stoi(l);//不用使用-'0'将其转换,直接使用stoi函数即可
book[tree[i].left] = ;
}
if(r == "-"){
tree[i].right = -;
} else {
tree[i].right = stoi(r);
book[tree[i].right] = ;
}
}
for(int i = ; i < n; i++) {
if(book[i] == ) {
root = i;
break;//没有出现的便是根!
}
}
queue<int> q;
q.push(root);
int cnt = , lastnode = ;
while(!q.empty()) {
int node = q.front();
q.pop();
if(node != -) {
lastnode = node;
cnt++;//记录层次遍历在-1出现之前的节点数
}else {
if(cnt != n)
printf("NO %d", root);
else
printf("YES %d", lastnode);
return ;
}
q.push(tree[node].left);//如果左右子节点为空,那么就将-1push进去了
q.push(tree[node].right);
}
return ;
}
//学习了!
1.根据输入建树,每个节点因为本身就是ID,左右如果是空节点,那么就赋值为-1.
2.根节点是怎么找到的呢?在建树输入的过程中,如果一个点没有出现,那么就是根节点,因为都在一棵树中!都是表示的是子节点,如果没出现,就表示它不是子节点,而是根节点!
3.如何去判断是否是CBT呢?使用层次遍历!并且记录当前层次遍历的个数,根据CBT的性质,如果当前出现空节点,但是遍历过的点数!=总结点数,那么就不是二叉树,可以画一个图试试!使用队列!
//学习了!
PAT 1110 Complete Binary Tree[判断完全二叉树]的更多相关文章
- 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 ...
- PAT 1110 Complete Binary Tree
Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...
- [二叉树建树&完全二叉树判断] 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 ...
- PAT甲级——1110 Complete Binary Tree (完全二叉树)
此文章同步发布在CSDN上:https://blog.csdn.net/weixin_44385565/article/details/90317830 1110 Complete Binary ...
- 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
1110 Complete Binary Tree (25)(25 分) Given a tree, you are supposed to tell if it is a complete bina ...
- 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 甲级 1110 Complete Binary Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805359372255232 Given a tree, you are ...
- 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 ...
随机推荐
- app服务端server端数据库设计
- js中如何跳出循环
1.for循环中我们使用continue:终止本次循环计入下一个循环,使用break终止整个循环. 2.而在jquery中 $.each使用return true 终止本次循环计入下一个循环,retu ...
- oracle dblink结合同义词的用法 PLS-00352:无法访问另一数据库
我从源库导出数据PCK报错如下:
- Windows下Nutch的配置
Nutch是一个开源的.Java实现的搜索引擎.它提供了我们运行自己的搜索引擎所需的全部工具. Nutch可以分为2个部分: 抓取部分crawler 抓取程序抓取页面并把抓取回来的数据做成反向索引 搜 ...
- php学习九:存取器&魔术方法
当声明属性的时候用到了private,那么的话只能本类中才能访问此属性,其他地方无法使用,这个时候需要给外界提供一些方法来使外界访问 方法一:存取器: 可以提供一个public的方法来使外界对类里面的 ...
- 《C#高级编程》学习笔记------什么是COM组件
COM是Component Object Model (组件对象模型)的缩写. 用户需要什么样的软件产品?这是一个多选题,但高效,健壮是肯定会被选中的.作为一名软件开发人员如何做才能满足用户的需要呢? ...
- Android英文文档翻译系列(4)——PopupWindow
public class PopupWindow extends Object //直接继承至Object java.lang.Object ↳ android.widget.PopupWindow ...
- poj_3258 二分法
题目大意 给定区间[0,L],在区间内给定N个数,加上区间的端点总共N+2个值.这N+2个数相邻的两个数之间有一个差值delta[i],现在可以从除去端点之外的这N个数中删除M个,使得剩余的N+2-M ...
- 【Mysql】大数据处理优化方法
1.应尽量避免在 where 子句中使用 != 或 <> 操作符,否则将引擎放弃使用索引而进行全表扫描. 2.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 orde ...
- struts2基础---->第一个Struts2程序
学习struts2的第一个程序,这里只会涉及到简单的代码编写.有一个夜晚我烧毁了所有的记忆,从此我的梦就透明了:有一个早晨我扔掉了所有的昨天,从此我的脚步就轻盈了. Struts的项目