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)的更多相关文章

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

  2. 1110 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 (25)-(判断是否为完全二叉树)

    题意:判断一个节点为n的二叉树是否为完全二叉树.Yes输出完全二叉树的最后一个节点,No输出根节点. 建树,然后分别将该树与节点树为n的二叉树相比较,统计对应的节点个数,如果为n,则为完全二叉树,否则 ...

  5. 【PAT甲级】1110 Complete Binary Tree (25分)

    题意: 输入一个正整数N(<=20),代表结点个数(0~N-1),接着输入N行每行包括每个结点的左右子结点,'-'表示无该子结点,输出是否是一颗完全二叉树,是的话输出最后一个子结点否则输出根节点 ...

  6. PAT (Advanced Level) 1110. Complete Binary Tree (25)

    判断一棵二叉树是否完全二叉树. #include<cstdio> #include<cstring> #include<cmath> #include<vec ...

  7. 1110 Complete Binary Tree

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

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

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

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

随机推荐

  1. vmware网络配置(NAt模式)

    学习参考: http://www.linuxidc.com/Linux/2017-03/141864.htm(讲解NAT模式) 配置: 1.在virtul Network Editor(虚拟网络编辑器 ...

  2. Redis的分布式配置

    Redis存在三种级别的分布式部署:主从复制.主从切换.集群配置,推荐使用主从切换模式. 主从复制 启动主服务:端口6379 启动从服务:端口6380, 配置文件中加上 slaveof 127.0.0 ...

  3. iOS获取某个日期后n个月的日期

    一.给一个时间,给一个数,正数是以后n个月,负数是前n个月: -(NSDate *)getPriousorLaterDateFromDate:(NSDate *)date withMonth:(NSI ...

  4. libhdfs的配置和使用

    测试环境:centos6.10,hadoop2.7.3,jdk1.8 测试代码:HDFSCSample.c #include "hdfs.h" #include <strin ...

  5. 【Head First Servlets and JSP】迷你MVC:JarDownload的完整实现

    1.首先,写一个download.html放至D:\apache-tomcat-7.0.77\webapps\JarDownload-v1. <!DOCTYPE HTML> <htm ...

  6. PHP搜索文件夹下全部文件

    搜索文件夹下全部文件 //搜索文件夹下全部文件,暂时不支持中文文件名 public function scanFile($path) { if (!is_dir($path)) return arra ...

  7. Go reflect反射

    Go语言中的反射非常强大,可以对string, int, struct, func...进行反射,使用起来也比较简单. 示例1:反射函数 package main import ( "fmt ...

  8. RequestMapping请求映射方式

    1.标准映射 规则: 1) @RequestMapping可以设置在类上,也可以设置在方法上 2) 请求的映射规则是:类上的RequestMapping + 方法上的RequestMapping 3) ...

  9. TortoiseSVN教程级别指南

    安装说明 开发人员强烈建议使用IDE中的SVN插件更加智能与人性化. 首先安装SVN客户端,windows一般选择乌龟客户端https://tortoisesvn.net/downloads.html ...

  10. 使用shell自动备份数据库

    全备份 #!/bin/sh #mysql地址 #检测用户是否手动输入了密码 mysql_host="" #mysql用户 mysql_user="" #mysq ...