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. jQuery:自学笔记(3)——操作DOM

    jQuery:自学笔记(3)——操作DOM 修改元素的属性 获取元素属性 设置元素属性 修改元素的内容 说明 有三种方式可以获取HTML元素的内容,分别是 ☐ text():设置或返回所选元素的文本内 ...

  2. Xshell访问kali配置

    1.安装虚拟机VMware Workstation12 PRO 2.在虚拟机上安装kali2.0 3.查看liunx的ip地址ifconfig 4.端口 协议 (1)RDP协议(桌面协议)3389端口 ...

  3. MVC,MVP和MVVM区别

    复杂的软件必须有清晰合理的架构,否则无法开发和维护. MVC(Model-View-Controller)是最常见的软件架构之一,业界有着广泛应用.它本身很容易理解,但是要讲清楚,它与衍生的 MVP ...

  4. 前端之HTML基础

    一.初识HTML 1.web服务的本质 方式一:服务端 import socket def main(): sock = socket.socket(socket.AF_INET, socket.SO ...

  5. Ubuntu 15.10环境下安装Hive

    1. 安装MySQL sudo tar -xzvf mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz sudo mv mysql-5.7.13-linux-glibc ...

  6. hdu1010感想

    杭电这道题是用dfs走迷宫问题,一直wa是因为没有将走过的地方标记,所以如果遇到走迷宫的问题一定要将走过的地方标记,如下: &&nx<n&&ny>=& ...

  7. JavaWeb Request和Response

    1. Request与Response 1.1. Web应用运行机制 到目前为止,我们已经掌握了Web应用程序的运行机制,现在学习的就是Web应用程序运行机制中很重要的内容 —— Request与Re ...

  8. C++中容器的使用(二)

    第一章容器 第1条:慎重选择容器类型. 标准STL序列容器:vector.string.deque和list. 标准STL关联容器:set.multiset.map和multimap. 非标准序列容器 ...

  9. StringTemplateLoader的用法

    作为一个模板框架,freemarker的功能还是很强大的.在模板处理方面,freemarker有多种形式,最常见的方式是将模板文件放在一个统一的文件夹下面,如下形式:Configuration cfg ...

  10. 普通神经网络和RNN简单demo (一)

    2017-08-04 花了两天时间看了下神经网络的一点基础知识,包括单层的感知机模型,普通的没有记忆功能的多层神经网咯,还有递归神经网络RNN.这里主要是参考了一个博客,实现了几个简单的代码,这里把源 ...