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

 #include <stdio.h>
#include <algorithm>
#include <set>
#include <string.h>
#include <vector>
#include <math.h>
#include <queue>
#include <iostream>
#include <string>
using namespace std;
const int maxn = ;
int n,k;
struct node{
int l=-,r=-;
}tree[maxn];
int vis[maxn]={};
int main(){
scanf("%d",&n);
getchar();
for(int i=;i<n;i++){
string c1,c2;
cin>>c1>>c2;
getchar();
int n1=-,n2=-;
if(c1!="-"){
n1=stoi(c1);
vis[n1]=;
}
if(c2!="-"){
n2=stoi(c2);
vis[n2]=;
}
tree[i].l=n1;
tree[i].r=n2;
}
int root=-;
for(int i=;i<n;i++){
if(vis[i]==){
root=i;
break;
}
}
int flag=,flag2=,now;
queue<int> q;
q.push(root);
while(!q.empty()){
now=q.front();
q.pop();
//printf("%d ",now);
if(tree[now].l!=-){
if(flag==)q.push(tree[now].l);
else {
flag2=;
break;
}
}
else{
flag=;
}
if(tree[now].r!=-){
if(flag==)q.push(tree[now].r);
else {
flag2=;
break;
}
}
else{
flag=;
}
/*if(now!=-1){
flag++;
flag2=now;
}
else{
if(flag==n){
printf("YES %d",flag2);
}
else{
printf("NO %d",root);
}
return 0;
}
q.push(tree[now].l);
q.push(tree[now].r);
*/
}
if(flag2==)printf("NO %d",root);
else printf("YES %d",now);
}

注意点:完全二叉树的判断就是看已经有节点没孩子了,后面节点却还有孩子,这树就不是完全二叉树。有三个点一直错误,一开始段错误,后来在结构体初始化了-1后答案错误,总找不到原因。后来才发现原来是读输入的时候错了,两位数就读不到了,不能用%c,要用string

PAT A1110 Complete Binary Tree (25 分)——完全二叉树,字符串转数字的更多相关文章

  1. 1110 Complete Binary Tree (25 分)

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

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

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

  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甲级——A1110 Complete Binary Tree【25】

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

  5. PAT 1110 Complete Binary Tree[判断完全二叉树]

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

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

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

  8. A1110. Complete Binary Tree

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

  9. 1110. Complete Binary Tree (25)

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

随机推荐

  1. 【Java并发编程】21、线程池ThreadPoolExecutor源码解析

    一.前言 JUC这部分还有线程池这一块没有分析,需要抓紧时间分析,下面开始ThreadPoolExecutor,其是线程池的基础,分析完了这个类会简化之后的分析,线程池可以解决两个不同问题:由于减少了 ...

  2. Android线程

    1.引言 在Android中,几乎完全采用了Java的线程机制,由于Android的特性,主线程只处理和界面相关的事情,子线程处理耗时操作.Android中扮演线程角色的有Thread.AsyncTa ...

  3. 初学HTML-5

    表格标签:用来给一堆数据添加表格语义. 格式:<table> <tr> <td></td> </tr> </table> tab ...

  4. 02--css背景与边框--css揭秘

    背景与边框 一 半透明边框 rgba/hsla颜色 1.难题 假设我们想给一个容器设置一层白色背景和一道半透明白色边框,body 的背景会从它的半透明边框透上来.我们最开始的尝试可能是这样的: #bo ...

  5. NODE获取节点删除空格的操作

    NODE节点操作有: object.parentNode:获取某子元素的父级: object.childNodes:是获取所有的子元素节点,返回数组类型: object.lastChild: 获取该元 ...

  6. Android View体系(五)从源码解析View的事件分发机制

    1.处理点击事件的方法 View的层级 我们知道View的结构是树形的结构,View可以放在ViewGroup中,这个ViewGroup也可以放到另一个ViewGroup中,这样层层的嵌套就组成了Vi ...

  7. Android事件总线(三)otto用法全解析

    前言 otto 是 Square公司发布的一个发布-订阅模式框架,它基于Google Guava 项目中的event bus模块开发,针对Android平台做了优化和加强.虽然Square已经停止了对 ...

  8. (网页)javaScript增删改查(转)

    转自CSDN: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> ...

  9. 前端路由简介以及vue-router实现原理

    后端路由简介 路由这个概念最先是后端出现的.在以前用模板引擎开发页面时,经常会看到这样 http://www.xxx.com/login 大致流程可以看成这样: 浏览器发出请求 服务器监听到80端口( ...

  10. asp.net学习——Response对象

    (2011-03-29 07:33:03) 转载▼ 标签: 杂谈 分类: asp.net学习 响应的缓冲输出:为了提高服务器的性能,asp.net向浏览器Write的时候默认并不会每Write一次都会 ...