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 ...
随机推荐
- re.match与re.search的区别
re.match与re.search的区别 re.match只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回None:而re.search匹配整个字符串,直到找到一个匹配. 实 ...
- kafka-manager监控工具的安装和使用
kafka-manager监控工具的使用 第一步:对kafkamanager进行下载并编译 此步骤略:可参照成功与否不详,https://www.jianshu.com/p/174b6eb10d9d ...
- NX二次开发-UFUN链表UF_MODL_create_list等用法
NX9+VS2012 #include <uf.h> #include <uf_modl.h> #include <uf_curve.h> #include < ...
- NX二次开发-弹出选择文件夹对话框
这个UFUN和NOPEN里没有对应的函数和类,要用C++的方法去做. #include "afxdialogex.h"//弹出选择文件夹对话框头文件 #include " ...
- JavaScript闭包和回调详解
一.闭包 闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现. 闭包有三个特性: 1.函数嵌套函数; 2.函数内部可以引用外部的参数和变量; 3.参 ...
- linux 重定向命令
标准输入,输出和错误 --------------------------------- 文件文件 描写叙述符 ----------------------------- ...
- class10_Frame 框架
最终运行效果图(程序见序号2): #!/usr/bin/env python # -*- coding:utf-8 -*- # ---------------------------------- ...
- hadoop 8088 看不到mapreduce 任务的执行状态
进到8088页面后,怎么看不到mapreudce的执行状态,有哪里需要配置的 解决办法: 在$HADOOP_HOME/conf/mapred-site.xml 在原来的配置文件基础之上添加: < ...
- 创建一个学生表student,默认的表空间为users,字段自定,同时为表的各个字段分别添加合适的约束,然后测试约束的验证状态。
create table student(id number(4) constraint prim_key primary key,name varchar(8) not null,sex varch ...
- JS对象 数组连接 concat() 方法用于连接两个或多个数组。此方法返回一个新数组,不改变原来的数组。 语法 arrayObject.concat(array1,array2,.arrayN)
concat() 方法用于连接两个或多个数组.此方法返回一个新数组,不改变原来的数组. 语法 arrayObject.concat(array1,array2,...,arrayN) 参数说明: 注意 ...