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 ...
随机推荐
- http,soap and rest
http://www.cnblogs.com/hyhnet/archive/2016/06/28/5624422.html http://www.cnblogs.com/bellkosmos/p/52 ...
- 0530JavaScript基础2
常用内置对象 所谓内置对象就是ECMAScript提供出来的一些对象,我们知道对象都是有相应的属性和方法 数组Array(部分相当于列表) 1.数组的创建方式 var colors = ['red', ...
- async/await方法解析
欲了解await,必须先了解Promise,可参考: http://www.cnblogs.com/yanze/p/6347646.html 支持度: ES6已支持Promise,ES7也决定支持aw ...
- iOS_XML与JSON解析
XML与JSON简介 XML 可扩展标记语言 用于标记电子文件使其具有结构性的标记语言,可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言 易读性高,编码手写难度小,数据量 ...
- Centos 一次卸载多个RPM包
rpm 不支持通配符,可以使用 xargs来接收多个变量 示例,一次性卸载所有 erlang先关的rpm包: rpm -qa | grep erlang | xargs rpm -e --nodeps
- java深入探究14-lucene
项目代码:链接:http://pan.baidu.com/s/1qXVcfCw 密码:apw1 01 回顾索引 定义:索引是对数据库表中一列或多列的值进行排序的一种结构 目的:加快对数据库表中记录的查 ...
- SSH或者SSM开发web,mysql数据库,数据库配置文件配置不当~数据库读写数据乱码问题解决办法。
相信,大家都有遇到过在传入一个中文string,debug自己的每一行代码时,都发现始终是没有乱码的(即:排除了,源码文件的编码格式是没问题的),但是数据进入数据库之后就是乱掉了. 那么很明显问题就出 ...
- java基础(2)-面向对象(2)
构造方法 构造方法特点 方法名与类名相同 方法名前没有返回值类型的声明(void也没有) 方法中不能使用return语句返回一个值 创建对象时自动调用并执行 如果类中没有自定义构造方法,则java调用 ...
- DataX-ElasticSearch(写)
DataX写入ElasticSearch 1 快速介绍 数据导入elasticsearch的插件 2 实现原理 使用elasticsearch的rest api接口, 批量把从reader读入的数据写 ...
- tp5 数据库Db增删改操作
添加数据insert $data = [ 'name_cn' => '张三', 'name_en' => 'jack', ]; $res = Db::name('style')->i ...