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. Spring-属性文件自身的引用03
  2. EJS学习(四)之语法规则下
  3. android mvp 作用,Android MVP与MVC的区别和理解
  4. poj 1308 Is It A Tree?
  5. nodejs 前端 返回数组给_互联网寒冬,一年经验字节跳动、虾皮、快手、拼多多前端面试总结...
  6. 使用def文件从dll导出和_declspec(dllexport)导出区别以及调用示例
  7. Visual Studio.NET 2008开发一册通--LINQ从基础到项目实战这本书怎么样
  8. 记录自己的第一个稍微有趣一点的小游戏
  9. 深入理解操作系统原理之文件系统
  10. 学堂在线计算机通信网络,第二章 数据通信的基础知识 计算机网络笔记 学堂在线 2.4差错控制...
  11. 软件项目管理测试文档,软件项目管理具体方法体系示例
  12. python跑完代码后怎么办_2017/06/14跑成功了的代码,FYI
  13. 腾讯开源运维 PaaS 平台
  14. Vagrant 安装 Centos7
  15. C++ 使用fdk-aac对音频编码
  16. 腾讯微信客服电话号码是多少呢/腾讯微信人工服务热线
  17. 微信小程序总结(阶段第一次总结)
  18. 重心法-判断一点是否在三角形内
  19. pyquery 使用说明(支持python 3)
  20. 移动磁盘显示无法访问设备未就绪,里面的资料如何找到

热门文章

  1. P3048 [USACO12FEB]Cow IDs
  2. 成员变量、类变量、局部变量的区别
  3. 华为、阿里巴巴、字节跳动 100+ Python 面试问题总结(五)
  4. label中的for属性
  5. 免杀技术以及木马处理
  6. 自己搭建网站需要租服务器吗,网站空间租用和自己搭建服务器
  7. 线程共享和协作(一):CPU核心数,线程数,时间片轮转机制解读
  8. 空间计量xsmle命令遇到(3200)error,
  9. 2440init.s完全解析
  10. 2012年 华为机试题