文章目录

  • 题目分析
  • 题目链接

题目分析


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

ac代码

#include<bits/stdc++.h>
using namespace std;
const int N = 30;
int n;
bool has_father[N]; //找根结点,根结点的特点是:没有父节点
int l[N], r[N];
int max_k;//点在数组中的最大下标
int max_id; //最后一个结点的编号//u是结点编号,k是结点下标
void dfs(int u ,int k){if( u == -1) return;//如果下标大于最大下标,就更新之//同时最后节点的编号max_id也随之更新if(k > max_k) max_k = k , max_id = u;dfs(l[u], 2*k);  //左子树,结点下标 2*kdfs(r[u], 2*k +1); 右子树,结点下标 2*k +1}int main(){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),has_father[l[i]] =true;if(b != "-") r[i] = stoi(b),has_father[r[i]] =true;}int root = 0;//根结点rootwhile(has_father[root]) root++;dfs(root , 1);if(max_k == n) cout<<"YES"<<" "<<max_id<<endl;else cout<<"NO"<<" "<<root<<endl;}

题目链接

PAT甲级1110 Complete Binary Tree

PAT甲级1110 Complete Binary Tree:[C++题解]判断完全二叉树相关推荐

  1. PAT甲级1064 Complete Binary Search Tree (30分):[C++题解]完全二叉搜索树BST

    文章目录 题目分析 题目链接 题目分析 思路: 第一步,构造含有n个结点的完全二叉树:第二步,将n个数值填入,使其满足二叉搜索树的性质. 对于第一步: 完全二叉树用一维数组可以存下,不过从根结点的下标 ...

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

  4. PAT甲级Invert a Binary Tree 柳神层序遍历的思路值得借鉴

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

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

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

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

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

  7. PAT A1102 Invert a Binary Tree

    PAT A1102 Invert a Binary Tree Sample Input: 8 1 - - - 0 - 2 7 - - - - 5 - 4 6 Sample Output: 3 7 2 ...

  8. PAT甲级1138 Postorder Traversal:[C++题解]前序遍历和中序遍历建树

    文章目录 题目分析 题目链接 题目分析 做过前面几道题,发现这道题就是一道模板题,递归建树即可. 还是使用笔者熟悉的hash表来找根,进行优化. 请移步至笔者的另一篇文章:PAT甲级1020 Tree ...

  9. [LeetCode] Binary Tree Postorder题解

    Binary Tree Postorder Given a binary tree, return the postorder traversal of its nodes' values. For ...

最新文章

  1. jQuery 选择器模糊匹配
  2. DataGridView使用小结
  3. 【编译原理】让我们来构建一个简单的解释器(Let’s Build A Simple Interpreter. Part 2.)(python/c/c++版)(笔记)
  4. Jquery日历编写小练习
  5. MEF学习笔记(6):出口和元数据
  6. linux安装mq报5724,linux下MQ简单配置手册.doc
  7. 如何登陆网页的back office_如何使用iPhone面容ID快速登陆应用或网页
  8. 用jQuery实现返回页面顶部的功能
  9. HttpServletRequest和ServletRequest的区别
  10. 目标检测——感受野的学习笔记
  11. html设置背景图片自动适应屏幕大小,css适应屏幕 CSS背景图片自适应屏幕大小
  12. Java实现 LeetCode 313 超级丑数
  13. Problem G: 切煎饼
  14. WEB前端 -- onfocus=quot;this.blur()quot;
  15. 双活数据中心架构分析及优缺点
  16. ML CV DM PP
  17. Android面试题集锦
  18. 跳蚤市场应用市场现状研究分析-
  19. android平台下基于ANativeWindow实现渲染bitmap图像
  20. 更改计算机用户名不能上网,电脑网络用户名改了连接不上怎么办

热门文章

  1. CentOS 安装Zabbix 手记
  2. nginx做正向代理
  3. CSS 魔法系列:纯 CSS 绘制基本图形(圆、椭圆等)
  4. JS 回车提交,兼容IE、火狐、Opera、Chrome、Safari……
  5. ASP.NET MVC 中宿主WCF Rest 服务的解决方法
  6. Event 系列: jquery event 源码
  7. java resume过时方法_学点开发|关于Java多线程用法解析
  8. 【Matlab 图像】bwlabel() 连通域及图像分割
  9. 【PC工具】大神开源项目-配色调色工具Colorpicker颜色选择器
  10. 【PC工具】PhotoScape简单绿色功能强大的照片编辑器