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 ...
随机推荐
- python代码{v: k for k, v in myArray.items()}是什么意思?
最近在扒vnpy的源码总能看到{v: k for k, v in ORDERTYPE_VT2HUOBI.items()}这样的源码,就是不知道什么意思 然后万能的google找到了Quora的一个类似 ...
- mysql 存储过程 随笔
CREATE PROCEDURE `g2`( in sts int,in type int, code varchar(20),in s int)begin declare i int; declar ...
- Download QT
http://download.qt.io/archive/qt/
- bzoj1001题解
[解题思路] 显然,这题的答案是这个网格图的最小割.根据最大流-最小割定理,我们可以用网络流算法来求其最小割,时间复杂度最小为O(V2√E). 特殊的,这个网格图是一个平面图,于是可以根据平面图最小割 ...
- NX二次开发-UFUN获得图纸页数量UF_DRAW_ask_num_drawings
#include <uf.h> #include <uf_draw.h> #include <uf_ui.h> UF_initialize(); //获得有多少张图 ...
- pycharm 参数、快捷键、调试模式
PyCharm参数.快捷键.调试模式 PyCharm设置参数 在运行Python脚本时,会经常遇到需要传入额外的参数来运行脚本. 例如下脚本1: #!/usr/bin/env python2 # *. ...
- (转)python资料汇总(建议收藏)零基础必看
摘要:没料到在悟空问答的回答大受欢迎,为方便朋友,重新整理汇总,内容包括长期必备.入门教程.练手项目.学习视频. 一.长期必备. 1. StackOverflow,是疑难解答.bug排除必备网站,任何 ...
- C不同变量类型存储大小引发的BUG
#include"stdio.h" typedef signed char int8; typedef unsigned char uint8; typedef signed sh ...
- Java父类强制转换子类原则
最近,微信群友在讨论子类父类的转换问题,其实不难,给大家用实例来说明一下就很明了了. 我们知道Java中子类转换成父类是没有任何问题的,那父类可以转换成子类吗? 来看下面这段程序: public cl ...
- 【CF516D】Drazil and Morning Exercise
题目 首先我们知道,在树上距离一个点最远的点一定是直径的两个端点之一 首先两遍\(\rm dfs\)把直径求出来,定义\(d(u)\)表示点\(u\)距离其最远点的距离,有了直径我们就能求出\(d\) ...