1110 Complete Binary Tree (25 分)

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<bits/stdc++.h>
using namespace std;
struct Node{int data;int l,r;
}gg[25];
int flag = 0, maxn = -1,ans;
void DFS(int root, int id)
{if(id > maxn){maxn = id;ans = root;}if(gg[root].l != -1)DFS(gg[root].l, id * 2);if(gg[root].r != -1)DFS(gg[root].r, id * 2 + 1);return ;
}
int main()
{int n;char s1[5],s2[5];int b[22] = {0};scanf("%d", &n);for(int i = 0 ;i < n; i++){scanf("%s %s",s1,s2);if(s1[0] == '-')gg[i].l = -1;else {int data = 0, now = 0;while(s1[now] != '\0')data = data * 10 + s1[now++]-'0'; b[data] = 1;gg[i].l = data;}if(s2[0] == '-')gg[i].r = -1;else {int data = 0, now = 0;while(s2[now] != '\0')data = data * 10 + s2[now++]-'0'; b[data] = 1;gg[i].r = data;}gg[i].data = i;}int root = 0;for(int i = 0; i < n; i ++){if(b[i] == 0){root = i;break;} }DFS(root, 1);if(maxn == n)printf("YES %d\n",ans);else printf("NO %d\n",root);return 0;
}

1110 Complete Binary Tree (25 分)(搜索)相关推荐

  1. 1110 Complete Binary Tree (25分)测试点2,3,4

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

  2. 1110 Complete Binary Tree (25 分)【难度: 一般 / 知识点: 判断完全二叉树】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805359372255232 建树并给其赋值,如果是完全二叉树,那么max ...

  3. 1110 Complete Binary Tree(甲级)

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

  4. PAT甲级1110 Complete Binary Tree:[C++题解]判断完全二叉树

    文章目录 题目分析 题目链接 题目分析 分析: 按照层序的顺序将完全二叉树存在下标从1开始的数组中.如果是完全二叉树,会将数组中1 ~ n这些位置填满,最大下标就是n,如果最大下标大于n,说明中间有空 ...

  5. 1102 Invert a Binary Tree (25 分)

    1102 Invert a Binary Tree (25 分) The following is from Max Howell @twitter: Google: 90% of our engin ...

  6. PAT甲级1123 Is It a Complete AVL Tree (30分):[C++题解]建立平衡树、bfs,判完全二叉树

    文章目录 题目分析 题目链接 题目分析 来源:pat网站 本题作为进阶题,它的基础知识点如下几题. PAT甲级1066 Root of AVL Tree (25分):[C++题解]建立平衡树(AVL树 ...

  7. C++学习之路 | PTA(甲级)—— 1043 Is It a Binary Search Tree (25分)(带注释)(精简)

    1043 Is It a Binary Search Tree (25分) A Binary Search Tree (BST) is recursively defined as a binary ...

  8. PAT (Advanced Level) Practice 1043 Is It a Binary Search Tree (25 分) 凌宸1642

    PAT (Advanced Level) Practice 1043 Is It a Binary Search Tree (25 分) 凌宸1642 题目描述: A Binary Search Tr ...

  9. PAT甲级1151 LCA in a Binary Tree (30 分):[C++题解]LCA、最低公共祖先、哈希表映射

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 和下面这道题几乎是同一题:PAT甲级1143 Lowest Common Ancestor (30 分):[C++题解]LCA.最低 ...

  10. PAT甲级1066 Root of AVL Tree (25分):[C++题解]建立平衡树(AVL树)

    文章目录 题目分析 题目链接 题目分析 图片来源:acwing 分析 平衡树(AVL树)是平衡二叉搜索树的简称,当然需要满足二叉搜索树的性质,左子树小于根,根小于等于右子树:然后还要满足平衡树的基本特 ...

最新文章

  1. Python3 中的最大整数和最大浮点数
  2. Scala Array中_ filter map的用法示例
  3. .NET Core开发日志——HttpClientFactory
  4. 大数据时代,怎么做全渠道的营销
  5. linux 修改时区_教你在Centos8中更改时区
  6. 数通手稿留档——Multicast
  7. summit_Linux注册简介开始,Open Hardware Summit寻求提交,等等
  8. android回收内存保存变量,android基础之onSaveInstanceState用法(一)保存容易被回收的自定义类的静态全局变量...
  9. cecos linux启动盘,CecOS 1.0 社区版企业云操作系统即将发布!
  10. 调用import win32com.client出错的解决办法
  11. 汇编语言跳转指令总结
  12. word参考文献乱码问题
  13. 如何把照片转成pdf文件,支持合并转换
  14. ios模拟器 安装ipa_安装ipa到模拟器
  15. 9、一篇文章带你看懂5G网络(接入网+承载网+核心网)
  16. Android移动开发基础
  17. 初学c语言试写的答题系统
  18. 在英特尔独立显卡上训练ResNet PyTorch模型
  19. 一个精美的登录界面原来是这样做的
  20. js实现天降字母打字小游戏

热门文章

  1. WPF版的Dock控件第二版完成
  2. 【Storm总结-6】Twitter Storm: DRPC简介
  3. 【笔记】时间片轮转 RR 进程调度算法(Java 实现)
  4. LinkedIn庄振运:从国家部委公务员到硅谷系统性能专家,创新是唯一主旋律
  5. 安全狗云备份爆笑段子~~~如果上天再给我一次机会
  6. EXP-00003解决
  7. linux管理员基础知识
  8. 【Unity笔记】UGUI的Image、RawImage控件
  9. 05-树7 堆中的路径
  10. 使用jpa报No query defined for that name错误