码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 1110 Complete Binary Tree


    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:

    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:

    NO 1

    判断是否是完全二叉树:首先树的高度和结点个数满足 n >= power(2, h - 1) && n <= (power(2, h) - 1),其次最下面一层的结点必须是从左到右依次排列的,即倒数第二层的结点不能只有右子树而没有左子树,可以只有左子树,而且如果序号在前的结点已经没有左右子树了,序号在后的结点就不能再有左右子树。

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. using namespace std;
    7. int n, root, cnt, last, h;
    8. string l, r;
    9. bool vis[50];
    10. bool flag;
    11. struct node {
    12. int val, left, right;
    13. } a[50];
    14. queueq;
    15. int power(int x, int y) {
    16. int ans = 1;
    17. while (y > 0) {
    18. if (y & 1) {
    19. ans *= x;
    20. }
    21. x *= x;
    22. y >>= 1;
    23. }
    24. return ans;
    25. }
    26. void dfs(int x, int d) {
    27. h = max(h, d);
    28. if (a[x].left != -1) {
    29. dfs(a[x].left, d + 1);
    30. }
    31. if (a[x].right != -1) {
    32. dfs(a[x].right, d + 1);
    33. }
    34. }
    35. int main() {
    36. cin >> n;
    37. for (int i = 0; i < n; i++) {
    38. cin >> l >> r;
    39. a[i].val = i;
    40. if (l == "-") {
    41. a[i].left = -1;
    42. } else {
    43. a[i].left = atoi(l.c_str());
    44. vis[atoi(l.c_str())] = 1;
    45. }
    46. if (r == "-") {
    47. a[i].right = -1;
    48. } else {
    49. a[i].right = atoi(r.c_str());
    50. vis[atoi(r.c_str())] = 1;
    51. }
    52. if (a[i].left == -1 && a[i].right != -1) {
    53. flag = 1;
    54. }
    55. }
    56. for (int i = 0; i < n; i++) {
    57. if (!vis[i]) {
    58. root = i;
    59. break;
    60. }
    61. }
    62. if (flag) {
    63. cout << "NO " << root;
    64. return 0;
    65. }
    66. dfs(root, 1);
    67. if (!(n >= power(2, h - 1) && n <= (power(2, h) - 1))) {
    68. cout << "NO " << root;
    69. return 0;
    70. }
    71. q.push(a[root]);
    72. bool child = 1;
    73. while (!q.empty()) {
    74. int u = q.front().val;
    75. cnt++;
    76. q.pop();
    77. if (a[u].left != -1) {
    78. q.push(a[a[u].left]);
    79. }
    80. if (a[u].right != -1) {
    81. q.push(a[a[u].right]);
    82. }
    83. if (a[u].left != -1 && !child) {
    84. flag = 1;
    85. }
    86. if (a[u].right == -1 && child) {
    87. child = 0;
    88. }
    89. if (cnt == n) {
    90. last = u;
    91. }
    92. }
    93. if (flag) {
    94. cout << "NO " << root;
    95. } else {
    96. cout << "YES " << last;
    97. }
    98. return 0;
    99. }

     

  • 相关阅读:
    【Unity】U3D TD游戏制作实例(四)建造防御塔:防御塔生成器、一个int代表多选框,圆上任意点位的坐标计算、制作防御塔预制件
    【GNN基础学习】图模块基本定义 || 图的邻接矩阵 || GNN中常见任务有哪些? || GNN消息传递方法 || 多层GCN有什么作用?
    2023Web前端面试题及答案(一)
    pytest-xdist分布式测试原理浅析
    Ubuntu crontab 遇到的sh脚本一些问题(手动执行可以,自动执行不行)
    MybatisPlus简介
    一次XGBoost性能优化-超线程影响运算速度
    波场地址解析易语言代码
    Go 言 Go 语,一文看懂 Go 语言文件操作
    本地vscode安装GPU版本PyTorch
  • 原文地址:https://blog.csdn.net/weixin_53199925/article/details/127444423
  • 最新文章
  • 【JVM】编译执行与解释执行的区别是什么?JVM 使用哪种方式?
    用 Hashids 优雅解决 C 端自增 ID 暴露问题
    V8引擎 精品漫游指南--Ignition篇(上) 指令 栈帧 槽位 调用约定 内存布局 基础内容
    LLVM Pass快速入门(四):代码插桩
    milkup:桌面端 markdown AI续写和即时渲染
    基于项目工程构建SBOM(软件物料清单)的研究
    鸿蒙应用开发UI基础第二节:鸿蒙应用程序框架核心解析与实操
    .NET 中如何快速实现 List 集合去重?
    扣子Coze实战:从0到1打造抖音+小红书热点监控智能体
    浅谈数据访问层
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号