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.

分析:用结构体存储每个结点,如果左孩子或右孩子为空用-1代替,之后根据完全二叉树的特点将结点全部存入数组中。

测试点2,3,4:因为结点可能是两位数,我之前用char类型去获取左右孩子结点,但是结点可能是两位数,所以需要用string

AC代码:

#include<iostream>
#include<algorithm>
using namespace std;
struct node{int left;int right;
}tree[20];
int res[20];
int n;
void dfs(int root,int index){if(index>n){return;}res[index]=root;if(tree[root].left!=-1){dfs(tree[root].left,index*2+1);}if(tree[root].right!=-1){dfs(tree[root].right,index*2+2);}return;
}
int main(){cin>>n;bool flag[n];fill(flag,flag+n,false);fill(res,res+20,-1);for(int i=0;i<n;++i){string a,b;//一定要注意节点可能是两位数 cin>>a>>b;if(a[0]=='-'){tree[i].left=-1;}else{int t=stoi(a);tree[i].left=t;flag[t]=true;}if(b[0]=='-'){tree[i].right=-1;}else{int t=stoi(b);tree[i].right=t;flag[t]=true;}}int root=0;for(int i=0;i<n;++i){//找根 if(flag[i]==false){root=i;break;}}dfs(root,0);bool tt=false;for(int i=0;i<n;++i){if(res[i]==-1){tt=true;break;}}if(tt){cout<<"NO"<<" "<<res[0];}else{cout<<"YES"<<" "<<res[n-1];}return 0;
}

1110 Complete Binary Tree (25分)测试点2,3,4相关推荐

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

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

  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. PAT甲级1123 Is It a Complete AVL Tree (30分):[C++题解]建立平衡树、bfs,判完全二叉树

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

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

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

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

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

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

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

最新文章

  1. word2vec 中的数学原理详解
  2. 数值分析 pdf_统计分析前,要做哪些数据准备工作?
  3. Effective Objective-C 2.0 编写高质量iOS与OS X代码的52个有效方法笔记-协议与分类...
  4. youcans 的 OpenCV 学习课—4.图像的叠加与混合
  5. R学习-- 数组和矩阵
  6. 面向对象(OOP)一
  7. 权限管理数据表设计说明
  8. mysql报表慢_mysql慢查询日志报表工具mysqlsla
  9. 使用 powershell 的 grep 过滤文本
  10. 配置Exchange邮箱完全访问权限
  11. 现代软件工程 第十五章 【稳定和发布阶段】练习与讨论
  12. 图画日记怎么画_画画日记(通用10篇)
  13. windows 批处理(bat) 修改文件md5 值
  14. 模拟CMOS集成电路设计中的电流基准源及用Cadence Virtuoso IC617设计并仿真有关电路
  15. 关于一维数组(小朋友都能明白的一维数组超详细介绍)
  16. 笔记本电脑上没有Home和End键如何使用组合键?
  17. 360度评估中的问题示范:如何提问
  18. ArcGIS 实验理论基础十五 空间查询
  19. wwbizsrv.exe-应用程序错误
  20. Java 面向对象学习一周小结

热门文章

  1. 基于matlab车辆距离识别
  2. 连接器半年度业绩预告 下半年业绩支撑点在哪
  3. Docker的Registry部署与使用
  4. 客户主数据-供应商主数据-业务伙伴
  5. 向正展厅云平台:推动数字化展示转型的不二之选
  6. python开发windows界面_pyui4win
  7. (七)云计算技术学习--OpenStack之Cinder和Swift
  8. 打造适合自己的知识库,循环提纯知识并让自己受益
  9. SQL SERVER EXCEPT、INTERSECT 用法
  10. 想要成为优秀外贸业务员,必须养成的九个好习惯