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[判断完全二叉树]的更多相关文章

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

  2. PAT 1110 Complete Binary Tree

    Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...

  3. [二叉树建树&完全二叉树判断] 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 ...

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

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

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

  6. 1110 Complete Binary Tree

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

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

  8. PAT 甲级 1110 Complete Binary Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805359372255232 Given a tree, you are ...

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

随机推荐

  1. IIS 使用多个https和通配证书解决方案

    环境:OS :WINDOWS 2008 IIS: IIS7 域名:三个二级域名 问题:由于一个网站只支持一个443,但可以通过更改配置得到绑定不同域名.但由于公用证书,所以问题出来.只能为一个二级域名 ...

  2. 关于BroadCastReceiver安全性的思考

    尊重原创:http://blog.csdn.net/yuanzeyao/article/details/38948863 BroadCastReceiver是Android 四大组件之中的一个,应用非 ...

  3. java.util.logging.Logger使用具体解释

    java.util.logging.Logger不是什么新奇东西了,1.4就有了,但是由于log4j的存在,这个logger一直沉默着,事实上在一些測试性的代码中,jdk自带的logger比log4j ...

  4. windows平台的游戏运行库

    每一个都在PC上玩过游戏的人,都知道要安装一些必备的游戏运行库,游戏才能运行,这里指的PC是特指Windows操作系统平台.一般来说最常见的运行库是DirectX.Microsoft Visual C ...

  5. JS 保存表单默认值 为空时自动填充默认值

    var inputArray = document.getElementsByTagName("input"); var strArray = []; ; i < input ...

  6. Apache服务器最新版下载、安装及配置(win版)

    Apache服务器最新版下载.安装及配置(win版) Apache的下载: 登录http://httpd.apache.org/download.cgi 这个地址,找到2.4.10,如下图位置:   ...

  7. java EE ME SE有什么关系

    1. Java SE(Java Platform,Standard Edition).Java SE 以前称为 J2SE.它允许开发和部署在桌面.服务器.嵌入式环境和实时环境中使用的 Java 应用程 ...

  8. [黑金原创教程] FPGA那些事儿《设计篇 III》- 图像处理前夕·再续

    简介 一本为入门图像处理的入门书,另外还教你徒手搭建平台(片上系统),内容请看目录. 注意 为了达到最好的实验的结果,请准备以下硬件. AX301开发板, OV7670摄像模块, VGA接口显示器, ...

  9. js 生成二维码图片

    1.用纯JavaScript实现的微信二维码图片生成器 QRCode.js是javascript实现二维码(QRCode)制作生成库. QRCode.js有着良好的跨浏览器兼容性(高版本使用HTML5 ...

  10. windows下的mysql迁移到linux下

    最近做毕业设计,需要把windows下的mysql移植到linux下 曾经有过在window下移植mysql数据库的经验,只需要把msql的数据文件复制到另一台安装mysql的机器的数据存放位置,然后 ...