PAT甲级——A1110 Complete Binary Tree【25】
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 (≤) 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 <queue>
#include <string>
using namespace std;
struct Node
{
int l, r;
}node;
int n;
int main()
{
cin >> n;
vector<Node>tree;
vector<bool>isRoot(n, true);
for (int i = ; i < n; ++i)
{
string s1, s2;
cin >> s1 >> s2;
if (s1 == "-")
node.l = -;
else
{
node.l = atoi(s1.c_str());
isRoot[node.l] = false;
}
if (s2 == "-")
node.r = -;
else
{
node.r = atoi(s2.c_str());
isRoot[node.r] = false;
}
tree.push_back(node);
}
int root = -;//根
for (int i = ; i < n && root==-; ++i)
if (isRoot[i])
root = i;
queue<int>q, temp;
q.push(root);
while (!q.empty())//进行层序遍历
{
int p = q.front();
q.pop();
temp.push(p);//保存弹出的数据
if (tree[p].l != -)
q.push(tree[p].l);
else
break;//出现空子节点,则打破了完全二叉树的规则
if (tree[p].r != -)
q.push(tree[p].r);
else
break;//出现空子节点,则打破了完全二叉树的规则
}
if (temp.size() + q.size() == n)//满足完全二叉树的要求
cout << "YES " << q.back() << endl;//最后压入的就是最后一个节点
else
cout << "NO " << root << endl;
return ;
}
PAT甲级——A1110 Complete Binary Tree【25】的更多相关文章
- PAT甲级——1110 Complete Binary Tree (完全二叉树)
此文章同步发布在CSDN上:https://blog.csdn.net/weixin_44385565/article/details/90317830 1110 Complete Binary ...
- 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 ...
- [二叉树建树&完全二叉树判断] 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 ...
- 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 ...
- PAT 甲级 1064 Complete Binary Search Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805407749357568 A Binary Search Tree ( ...
- pat 甲级 1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)
1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a bin ...
- pat 甲级 1064 ( Complete Binary Search Tree ) (数据结构)
1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a binar ...
随机推荐
- 揭秘!2周实现上云上市,阿里云SaaS上云工具包如何打造新云梯?
提到“上云”,很多人会理解成上IaaS,比如买一些计算.存储和网络云产品,把自己的应用系统部署上去.这的确是通常意义的上云.但对SaaS而言,需要从产品.商业.服务,三个维度考虑SaaS伙伴和客户的痛 ...
- 在vue中使用handsontable
1.使用npm安装 npm install handsontable @handsontable/vue 2.定义结构 <hot-table :settings="hotSetting ...
- delphi Sqlite
Delphi中SQLite如何读写二进制字段(Blob类型) 在Delphi中,有大量的组件可以操作SQLite数据库,如UniDAC就是其中一个比较优秀的,当然还有ASQLite3Component ...
- NX二次开发-UFUN添加工程图投影视图UF_DRAW_add_orthographic_view
NX9+VS2012 #include <uf.h> #include <uf_draw.h> #include <uf_obj.h> #include <u ...
- nginx+keepalive 实现高可用负载均衡方案
转:http://ju.outofmemory.cn/entry/52165 主nginx负载均衡器:172.26.11.99 (通过keepalived配置了VIP:172.26.11.101供外 ...
- hdu多校第九场 1006 (hdu6685) Rikka with Coin 暴力
题意: 有一些1毛,2毛,5毛,1块的钢镚,还有一些价格不同的商品,现在要求你带一些钢镚,以保证这些商品中任选一件都能正好用这些钢镚付账,问最少带多少钢镚. 题解: 对于最优解,1毛的钢镚最多带1个, ...
- AdaBoost笔记之原理
转自:https://www.cnblogs.com/ScorpioLu/p/8295990.html 一.Boosting提升算法 AdaBoost是典型的Boosting算法,属于Boosting ...
- 基于Netty的RPC架构学习笔记(十一):粘包、分包分析,如何避免socket攻击
文章目录 问题 消息如何在管道中流转 源码解析 AbstractNioSelector.java AbstractNioWorker.java NioWorker.java DefaultChanne ...
- hexo next主题深度优化(七),cdn加速。
文章目录 注: 正题: 免费cdn 收费cdn 个人博客:https://mmmmmm.me 源码:https://github.com/dataiyangu/dataiyangu.github.io ...
- 绿色版mysql 免安装使用(转载)
MySQL绿色版的安装(mysql-5.6.22-win32.zip) Posted on 2015-01-31 23:21 卒子 阅读(10739) 评论(2) 编辑 收藏 由于工作需要最近要开始研 ...