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:

  1. 9
  2. 7 8
  3. - -
  4. - -
  5. - -
  6. 0 1
  7. 2 3
  8. 4 5
  9. - -
  10. - -

Sample Output 1:

  1. YES 8

Sample Input 2:

  1. 8
  2. - -
  3. 4 5
  4. 0 6
  5. - -
  6. 2 3
  7. - 7
  8. - -
  9. - -

Sample Output 2:

  1. NO 1

  1. #include <stdio.h>
  2. #include <algorithm>
  3. #include <set>
  4. #include <string.h>
  5. #include <vector>
  6. #include <math.h>
  7. #include <queue>
  8. #include <iostream>
  9. #include <string>
  10. using namespace std;
  11. const int maxn = ;
  12. int n,k;
  13. struct node{
  14. int l=-,r=-;
  15. }tree[maxn];
  16. int vis[maxn]={};
  17. int main(){
  18. scanf("%d",&n);
  19. getchar();
  20. for(int i=;i<n;i++){
  21. string c1,c2;
  22. cin>>c1>>c2;
  23. getchar();
  24. int n1=-,n2=-;
  25. if(c1!="-"){
  26. n1=stoi(c1);
  27. vis[n1]=;
  28. }
  29. if(c2!="-"){
  30. n2=stoi(c2);
  31. vis[n2]=;
  32. }
  33. tree[i].l=n1;
  34. tree[i].r=n2;
  35. }
  36. int root=-;
  37. for(int i=;i<n;i++){
  38. if(vis[i]==){
  39. root=i;
  40. break;
  41. }
  42. }
  43. int flag=,flag2=,now;
  44. queue<int> q;
  45. q.push(root);
  46. while(!q.empty()){
  47. now=q.front();
  48. q.pop();
  49. //printf("%d ",now);
  50. if(tree[now].l!=-){
  51. if(flag==)q.push(tree[now].l);
  52. else {
  53. flag2=;
  54. break;
  55. }
  56. }
  57. else{
  58. flag=;
  59. }
  60. if(tree[now].r!=-){
  61. if(flag==)q.push(tree[now].r);
  62. else {
  63. flag2=;
  64. break;
  65. }
  66. }
  67. else{
  68. flag=;
  69. }
  70. /*if(now!=-1){
  71. flag++;
  72. flag2=now;
  73. }
  74. else{
  75. if(flag==n){
  76. printf("YES %d",flag2);
  77. }
  78. else{
  79. printf("NO %d",root);
  80. }
  81. return 0;
  82. }
  83. q.push(tree[now].l);
  84. q.push(tree[now].r);
  85. */
  86. }
  87. if(flag2==)printf("NO %d",root);
  88. else printf("YES %d",now);
  89. }

注意点:完全二叉树的判断就是看已经有节点没孩子了,后面节点却还有孩子,这树就不是完全二叉树。有三个点一直错误,一开始段错误,后来在结构体初始化了-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. FireFox升级后FireBug不能使用

    今天发现,火狐浏览器从49.0.2升级到50.0.2之后,firebug的js调试被禁用了,果断去找49.0.2的版本. 链接: https://ftp.mozilla.org/pub/firefox ...

  2. php中一个字符占用几个字节?

    先看看字符与字节有什么区别: (一)“字节”的定义 字节(Byte)是一种计量单位,表示数据量多少,它是计算机信息技术用于计量存储容量的一种计量单位. (二)“字符”的定义 字符是指计算机中使用的文字 ...

  3. Linux常用系统命令

    致歉:各位看到此博客的朋友们 因为命令的数量挺多的很多命令也都很简单  我就总结了一下具体的命令和这个命令是做什么的,主要的使用方法是链接到http://man.linuxde.net/的网站的,请各 ...

  4. Jenkins报错'Gradle build daemon disappeared unexpectedly'的问题解决

    在将项目集成到 Jenkins 后,经常会出现不稳定的构建,Jenkins 控制台输出的错误信息为:Gradle build daemon disappeared unexpectedly (it m ...

  5. Java并发编程(十二)Callable、Future和FutureTask

    一.Callable与Runnable 先说一下java.lang.Runnable吧,它是一个接口,在它里面只声明了一个run()方法: public interface Runnable { pu ...

  6. 监听软件异常崩溃并且保持日志--CrashHandler编写自己的异常捕获类

    平时写代码,我们可能会抛出各种异常,这些异常有些是我们测试过程中发现进行解决的,但是也有一些异常是我们未知的,不论是代码的逻辑问题还是Android本身底层的一些bug,我们都需要及时了解并进行解决. ...

  7. JS获取当前星期几的简易写法

    var str = "今天是星期" + "日一二三四五六".charAt(new Date().getDay()); mark在此,方便日后复制 原文https ...

  8. PL/SQL 删除主键 ORA-02443: 无法删除约束条件-不存在的约束条件

    在PL/SQL developer中删除一个表的主键,然后把另外一个字段设置成主键,删除的过程中报错:ORA-02443 我遇到这个问题出现的背景是: alter table saleqtya dro ...

  9. Oracle 表操作(转)

    1.增加新字段:alter table table_name add (name varchar(20) default 'http://www.zangjing.net/');. 2.修改表字段:a ...

  10. PHP Excel导入数据到MySQL数据库

    数据导出已经有了,怎么能没有数据导入呢,同样使用TP5框架,首先需要下载phpexcel.zip,放到第三方类库目录vendor目录下,然后有一个页面可以让你选择要导入的Excel文件,然后点击导入按 ...