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 ...
随机推荐
- 分享tiny4412,emmc烧录u-boot, 支持fastboot模式烧写emmc【转】
本文转载自:http://www.arm9home.net/read.php?tid-80810.html 分享tiny4412,emmc烧录u-boot, 支持fastboot模式烧写emmc ...
- 2.EF的数据审计日志
转载:采用EntityFramework.Extended 对EF进行扩展(Entity Framework 延伸系列2) 数据审计日志: 先说一下这个审计的概念,就是对所有的实体的操作(增,删,改) ...
- WPF,Silverlight与XAML读书笔记(3) - 标记扩展
hystar的.Net世界 博客园 首页 新闻 新随笔 联系 管理 订阅 随笔- 103 文章- 0 评论- 107 WPF,Silverlight与XAML读书笔记(3) - 标记扩展 说 ...
- 软-RAID 5组建
图文版raid5组建之软RAID [复制链接] 发表于 2007-3-6 09:19 | 来自 51CTO网页 [只看他] 楼主 硬件raid5的组建和使用,基本上说完 ...
- uploadify3.2.1版插件在ASP.NET中的使用
0.先去官网下载插件 下载uploadify3.2.1插件 解压后只需要一下文件: (1) jQuery.uploadify.min.js (2) uploadify.css (3) uploadif ...
- Java 数组的 12 个方法
1. 声明一个数组 String[] aArray = new String[5]; String[] bArray = {"a","b","c&q ...
- 产生冠军(toposort)
http://acm.hdu.edu.cn/showproblem.php?pid=2094 #include <stdio.h> #include <iostream> #i ...
- JavaScript--编程
第一步:把注释语句注释. 第二步:编写代码,在页面中显示 “系好安全带,准备启航--目标JS”文字: 第三步:编写代码,在页面中弹出提示框“准备好了,起航吧!” 提示: 可以把弹框方法写在函数里. 第 ...
- linux命令(001) -- chkconfig
一.准备知识 在说明chkconfig命令的用途之前,有必要先了解一下Linux系统中/etc/rc[0-6].d目录的用途. 众所周知,在Linux系统定义了7种不同的启动级别,这7种启动级别的含义 ...
- Linux添加用户组和删除用户组
1.添加用户组使用groupadd命令添加用户组:groupadd group_name此操作需由系统管理员进行.2.删除用户组使用groupdel命令删除用户组:groupdel group_nam ...