PAT 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:
9
7 8
- -
- -
- -
0 1
2 3
4 5
- -
- -
Sample Output 1:
YES 8
Sample Input 2:
8
- -
4 5
0 6
- -
2 3
- 7
- -
- -
Sample Output 2:
NO 1
#include<iostream>
#include<vector>
#include<string>
using namespace std;
vector<int> tree(21,-1), Left(20,-1), Right(20,-1), record(20,0);
int n;
void buildtree(int root){
if(Left[tree[root]]!=-1){
if(2*root+1>19)
return;
tree[2*root+1]=Left[tree[root]];
buildtree(2*root+1);
}
if(Right[tree[root]]!=-1){
if(2*root+2>19)
return;
tree[2*root+2]=Right[tree[root]];
buildtree(2*root+2);
}
}
int main(){
cin>>n;
string l, r;
for(int i=0; i<n; i++){
cin>>l>>r;
if(l[0]!='-'){
Left[i]=stoi(l);
record[Left[i]]=1;
}
if(r[0]!='-'){
Right[i]=stoi(r);
record[Right[i]]=1;
}
}
int i=0;
for(; i<n; i++)
if(record[i]==0)
break;
tree[0]=i;
buildtree(0);
int cnt=0;
for(int i=0; i<=20; i++)
if(tree[i]==-1)
if(i==n){
cout<<"YES "<<tree[n-1]<<endl;
return 0;
}
else{
cout<<"NO "<<tree[0]<<endl;
return 0;
}
}
PAT 1110 Complete Binary Tree的更多相关文章
- PAT 1110 Complete Binary Tree[判断完全二叉树]
1110 Complete Binary Tree(25 分) Given a tree, you are supposed to tell if it is a complete binary tr ...
- PAT 1110 Complete Binary Tree[比较]
1110 Complete Binary Tree (25 分) Given a tree, you are supposed to tell if it is a complete binary t ...
- PAT甲级——1110 Complete Binary Tree (完全二叉树)
此文章同步发布在CSDN上:https://blog.csdn.net/weixin_44385565/article/details/90317830 1110 Complete Binary ...
- 1110 Complete Binary Tree (25 分)
1110 Complete Binary Tree (25 分) Given a tree, you are supposed to tell if it is a complete binary t ...
- [二叉树建树&完全二叉树判断] 1110. Complete Binary Tree (25)
1110. Complete Binary Tree (25) Given a tree, you are supposed to tell if it is a complete binary tr ...
- 1110 Complete Binary Tree
1110 Complete Binary Tree (25)(25 分) Given a tree, you are supposed to tell if it is a complete bina ...
- PAT 甲级 1110 Complete Binary Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805359372255232 Given a tree, you are ...
- PAT Advanced 1110 Complete Binary Tree (25) [完全⼆叉树]
题目 Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each ...
- PAT A1110 Complete Binary Tree (25 分)——完全二叉树,字符串转数字
Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...
随机推荐
- BNU 13064 Dice (I) 前缀和优化DP
Dice (I) You have N dices; each of them has K faces numbered from 1 to K. Now you have arranged th ...
- oc84--单利
// Tools.h #import <Foundation/Foundation.h> @interface Tools : NSObject<NSCopying, NSMutab ...
- Xshell配色方案啊【学习笔记】
自己移植从putty版本移植到Xshell的配色方案,效果不错,看上去挺舒服. [myisayme] text(bold)=eaeaea magenta(bold)=ff55ff text=fffff ...
- Windows(7/8/10)搭建kibana 6.x版本(elasticsearch的可视化服务)
在搭建kibana之前,我们先了解下什么是kibana Kibana 是一款开源的数据分析和可视化平台,它是 Elastic Stack 成员之一,设计用于和 Elasticsearch 协作.您可以 ...
- python django简单操作
准备: pip3 install django==1.10.3 cmd django-admin startproject guest 创建一个guest的项目 cd guest manage. ...
- WPF PasswordBox MVVM 实现
由于PasswordBox.Password属性非依赖属性,所以不能作为绑定的目标,以下是本人的MVVM实现方法. PasswordBox.Password与TextBox.Text同步,TextBo ...
- (转)Vue 爬坑之路(三)—— 使用 vue-router 跳转页面
使用 Vue.js 做项目的时候,一个页面是由多个组件构成的,所以在跳转页面的时候,并不适合用传统的 href,于是 vue-router 应运而生. 官方文档: https://router.vue ...
- 在已有spring的基础上集成hibernate
1.导入hibernate的包和spring的包 hibernate3.hibernate-jpa-2.0-api-.必须的包,log4j,log4j配置文件 1.1 导入Spring的依赖包 ...
- Android 使用MySQL直接访问数据库
在实际项目中,一般很少直接访问MySQL数据库,一般情况下会通过http请求将数据传送到服务端,然后在服务端连接mysql数据库. 在android 中,会通过使用Jdbc 连接MySQL 服务器 p ...
- Ajax——异步基础知识(一)
基础概念 1.异步请求可以做到偷偷向服务器发送请求,而页面却不刷新 2.get异步请求传递参数是通过url追加键值对的方式 3.post异步请求比较特殊,需要设置请求的类型 User-Agent:浏览 ...