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 "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
判断是否是完全二叉树,用字符串读入结点,然后转换,层序遍历,遇到儿子是空的就停止,看看队列里是否是n个结点。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,v[],root = -,q[],head = ,tail = ;
char l[],r[];
struct Node
{
int left,right;
}s[];
int main()
{
cin>>n;
for(int i = ;i < n;i ++)
{
cin.get();
cin>>l;
if(strcmp(l,"-") == )s[i].left = -;
else
{
int d = ;
for(int j = ;l[j];j ++)
d = d * + l[j] - '';
s[i].left = d;
v[d] = ;
}
cin.get();
cin>>r;
if(strcmp(r,"-") == )s[i].right = -;
else
{
int d = ;
for(int j = ;r[j];j ++)
d = d * + r[j] - '';
s[i].right = d;
v[d] = ;
}
}
for(int i = ;i < n;i ++)
if(!v[i])
{
root = i;
break;
}
q[tail ++] = root;
while(head < tail)
{
if(s[q[head]].left != -)q[tail ++] = s[q[head]].left;
else break;
if(s[q[head]].right != -)q[tail ++] = s[q[head]].right;
else break;
head ++;
}
if(tail == n)cout<<"YES "<<q[tail - ];
else cout<<"NO "<<root;
}
1110. Complete Binary Tree (25)的更多相关文章
- [二叉树建树&完全二叉树判断] 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 (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 (25)-(判断是否为完全二叉树)
题意:判断一个节点为n的二叉树是否为完全二叉树.Yes输出完全二叉树的最后一个节点,No输出根节点. 建树,然后分别将该树与节点树为n的二叉树相比较,统计对应的节点个数,如果为n,则为完全二叉树,否则 ...
- 【PAT甲级】1110 Complete Binary Tree (25分)
题意: 输入一个正整数N(<=20),代表结点个数(0~N-1),接着输入N行每行包括每个结点的左右子结点,'-'表示无该子结点,输出是否是一颗完全二叉树,是的话输出最后一个子结点否则输出根节点 ...
- PAT (Advanced Level) 1110. Complete Binary Tree (25)
判断一棵二叉树是否完全二叉树. #include<cstdio> #include<cstring> #include<cmath> #include<vec ...
- 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甲级——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 ...
随机推荐
- iOS Swift 熊猫🐼跑酷 第一个小项目
前言:想用swift 写个小游戏 慢慢转化 能写出 ARKit来.但是又不能一口吃个胖子,慢慢来,在网络视频教程中撸了视频教学,断断续续看了半个多月,基本实现了 游戏主角
- HTML系列(1)简介
开始整理html的知识. (1)HTML HTML 是用来描述网页的一种语言. 1.HTML指的是超文本标记语言: HyperText Markup Language 2.HTML不是 ...
- django大全
数据库配置: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME':'dbname', 'USER': 'ro ...
- iOS_UIAlertController
ViewController.m文件 #import "ViewController.h" @interface ViewController () // @property (n ...
- FTH: (7156): *** Fault tolerant heap shim applied to current process. This is usually due to previous crashes. ***
这两天在Qtcreator上编译程序的时候莫名其妙的出现了FTH: (7156): *** Fault tolerant heap shim applied to current process. T ...
- INSPIRED启示录 读书笔记 - 第9章 产品副经理
发现帮手 从本质上讲,产品就是创意,产品经理的职责是想出好点并加以实现.我们需要好点子,有些想法是我们自己的创意,但如果仅依靠自己,就会严重限制创意的发挥 做产品要找公司最聪明的人合作,发现公司里潜在 ...
- RabbitMQ死信队列
关于RabbitMQ死信队列 死信队列 听上去像 消息“死”了 其实也有点这个意思,死信队列 是 当消息在一个队列 因为下列原因: 消息被拒绝(basic.reject/ basic.nac ...
- Linux学习笔记001——win下安装Linux虚拟机
我研二之前算是一个纯粹的计算机小白,因为某些原因开始接触了计算机方面的知识. Linux系统也就是前几个月才听说,因某些需求需要在Linux环境下运行.纯的Linux系统不太现实, 所以在他人帮助和自 ...
- 获取浏览器的相关信息(navigator)
* 智能机浏览器版本信息: * */ var browser = { versions: function() { var u = navigator.userAgent + navigator.ap ...
- Linux嵌入式 -- 内核 - 内存管理
1. 逻辑地址 线性地址 物理地址 段式管理: 16位CPU,20根地址总线,可寻址1M内存,但是只有16位的寄存器,64K. 逻辑地址 = 段基地址 + 段内偏移地址 物理地址 PA = 段 ...