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. elasticsearch安装之各种坑

    我用的是centos6.5,安装elasticsearch5.2.0 首先不说了,安装JDK1.8,下载elasticsearch5.2.0 https://www.elastic.co/downlo ...

  2. CSS-高度塌陷问题

    目录 CSS-高度塌陷问题 表现 产生的原因 高度塌陷的解决办法: BFC相关 CSS-高度塌陷问题 表现 例如: HTML: <div class="first"> ...

  3. SQL Server: create table sql script

    ---摇奖observeh数据库设计 Function getSpace lottery /* -- Author:geovindu 涂聚文 -- Date: 20180427 为了自写生成代码.根据 ...

  4. CSS外观属性

    CSS外观属性 color 文本颜色 line-height 行间距 text-aline水平对齐方式 text-indent 首行缩进 letter-spacing字间距 word-spacing ...

  5. HTML 简单归纳 -- 前端知识

    web前端 Internet:是一个全球性的计算机互联网络,中文名称“因特网”.“国际互联网”.“网际网”等等: Internet提供的服务:http.ftp.Telnet.email.www.bbs ...

  6. 【读书笔记】iOS-Interface Builder

    IBOutlet或IBAction符号对编译不产生任何影响,它们只是标记,用于告诉Xcode这些对象可以和UI控件进行关联,以便于在编辑Interface Builder上的UI控件的时候Xcode可 ...

  7. 根据需要扩展java中的ThreadPoolExecutor

    经常被重写的三个方法 ThreadPoolExecutor是可扩展的,通过查看源码可以发现,它提供了几个可以在子类化中改写的方法:beforeExecute,afterExecute,terminat ...

  8. 修改eclipse的背景色(转载)

    eclipse操作界面默认颜色为白色.对于我们长期使用电脑编程的人来说,白色很刺激我们的眼睛,所以我经常会改变workspace的背景色,使眼睛舒服一些. 设置方法如下: 1.打开window-> ...

  9. Android View体系(八)从源码解析View的layout和draw流程

    前言 上一篇文章我们讲了View的measure的流程,接下来我们讲下View的layout和draw流程,如果你理解了View的measure的流程,那这篇文章自然就不在话下了. 1.View的la ...

  10. TCP连接之报文首部

    在面试时,会经常被问到TCP报文的一些细节,可以说TCP报文是不少企业用来考察面试者对网络的掌握程度的一道题目. TCP连接作为网络传输的一个环节,是不可或缺的一部分.例如,OSI七层模型的应用层HT ...