https://pintia.cn/problem-sets/994805342720868352/problems/994805359372255232

建树并给其赋值,如果是完全二叉树,那么maxid一定为n,否则则不是完全二叉树。
找根节点,就找到入度为0的点就是根节点。

#include<bits/stdc++.h>
using namespace std;
int l[30],r[30];
int n,root,last,maxid,d[25];
void dfs(int u,int k)
{if(maxid<k){maxid=k,last=u;}if(l[u]!=-1) dfs(l[u],k*2);if(r[u]!=-1) dfs(r[u],k*2+1);
}
int main(void)
{memset(l,-1,sizeof l);memset(r,-1,sizeof r);cin>>n;for(int i=0;i<n;i++){string a,b; cin>>a>>b;if(a!="-") l[i]=stoi(a),d[stoi(a)]++;if(b!="-") r[i]=stoi(b),d[stoi(b)]++;}for(int i=0;i<n;i++) if(d[i]==0) root=i;dfs(root,1);if(maxid==n) printf("YES %d",last);else printf("NO %d",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(甲级)

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

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

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

  4. 1102 Invert a Binary Tree (25 分)

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

  5. 1066 Root of AVL Tree (25 分)【难 / 知识点: 平衡树 未完成】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805404939173888 平衡树之前学过,不过有忘完了,有时间补吧

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

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

  7. PAT A1110 Complete Binary Tree ——雨打梨花深闭门

    PAT A1110 Complete Binary Tree 思路不好,平添烦恼 判断是不是完全二叉树,一开始想的是这种树前n-1层是满的,最后一层可能有空位,所以遍历一下记录每层的数量,前几层根据数 ...

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

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

最新文章

  1. Nginx负载均衡:分布式/热备Web Server的搭建
  2. WeUI用Toast组件显示Loading
  3. scala教程之:可见性规则
  4. Word 2010 制作文档结构之图标自动编号设置
  5. 聊聊缓存机制:双写兜兜转转,又回到了串行化
  6. 关闭mysql的安全模式_mysql开启和关闭安全模式
  7. WebService学习总结(5)——WebService常见开发框架比较
  8. 吴恩达深度学习4.2练习_Convolutional Neural Networks_the Happy House(Keras)
  9. Apache2.4使用require指令进行访问控制--允许或限制IP访问/通过User-Agent禁止不友好网络爬虫...
  10. 【杂文】【演化学习】platEMO提取缓存Data
  11. POCO c++ 使用例子
  12. IDEA右键新建时没有Java Class选项
  13. 基于OptiSystem的波分+时分复用混合光网络系统
  14. 单片微机计算机原理与接口技术高峰,单片微机原理与接口技术
  15. 【应用安全】垃圾短信电话不断?手机变卡变慢?可能是共享充电宝的锅……
  16. 外卖cps项目的玩法淘宝客经验分享
  17. qiankun前端微服务框架-同一服务器同一端口
  18. HDU 1859 最小长方形
  19. 2244小游戏HTML5小游戏,2244小游戏:王者荣耀干扰正常游戏扣多少分 举报不成功是什么意思...
  20. Linux 危险的 rm 命令,替换 trash

热门文章

  1. OS_FLAG.C(3)
  2. 机器学习-特征工程中的样本不均衡处理方法
  3. 洛谷 P2590 [ZJOI2008]树的统计
  4. PHP-fpm 优化问题
  5. Linux 下crontab简单使用
  6. Mathematica数据处理(11)--标签
  7. C++知识回顾之__stdcall、__cdcel和__fastcall三者的区别
  8. 聚类算法:Hierarchical Clustering层次聚类
  9. Hyperledger Besu(1)“系统框架”
  10. 2-Authentication Framework Chain of Trust