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】的更多相关文章

  1. PAT甲级——1110 Complete Binary Tree (完全二叉树)

    此文章同步发布在CSDN上:https://blog.csdn.net/weixin_44385565/article/details/90317830   1110 Complete Binary ...

  2. PAT 甲级 1110 Complete Binary Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805359372255232 Given a tree, you are ...

  3. 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 ...

  4. [二叉树建树&完全二叉树判断] 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 ...

  5. 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 ...

  6. PAT 甲级 1064 Complete Binary Search Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805407749357568 A Binary Search Tree ( ...

  7. pat 甲级 1064. Complete Binary Search Tree (30)

    1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  8. PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)

    1064 Complete Binary Search Tree (30 分)   A Binary Search Tree (BST) is recursively defined as a bin ...

  9. pat 甲级 1064 ( Complete Binary Search Tree ) (数据结构)

    1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a binar ...

随机推荐

  1. unity 打包Apk生成签名证书keystore

    进行Android项目开发中想要将androidapp导出为apk的时候需要选择一个数字证书,即keystore文件(android.keystore),它用来对我们的APP进行签名,是导出APP的一 ...

  2. bzoj1217: [HNOI2003]消防局的设立 [树形dp]

    Description 2020年,人类在火星上建立了一个庞大的基地群,总共有n个基地.起初为了节约材料,人类只修建了n-1条道路来连接这些基地,并且每两个基地都能够通过道路到达,所以所有的基地形成了 ...

  3. 线性dp——hdu6578经典dp

    多校第一场第一题,这种类型的dp之前做过两题,状态转移一般是从当前状态往后推的 很经典的dp,不过很卡时间 /* 定义 dp[t][i][j][k]代表填完前 t 个位置后,{0, 1, 2, 3} ...

  4. spark在collect收集数据的时候出现outOfMemoryError:java heap space

    spark的collect是action算子,所有最后会以数组的形式返回给driver端,当数据太大的时候就会出现堆内存溢出.OutofMemoryError:java heap space. 在sp ...

  5. 几道noip2018提高组初赛的题

    以下做法来均自llj @Nicodafagood 一.单项选择题 7. 在一条长度为 1 的线段上随机取两个点,则以这两个点为端点的线段的期望 长度是( ).A. 1 / 2B. 1 / 3C. 2 ...

  6. Feign Request header is too large

    Feign远程调用时数据量过大报错 看异常提示猜测Feign在请求其他服务时,将数据存在了header,导致数据量过大报错 MultiValueMap<String, String> pa ...

  7. NX二次开发-NXOPEN工程图导出CAD图纸DxfdwgCreator *dxfdwgCreator1;

    没有什么可以看的,NXOPEN直接录制一下导出CAD就可以了.录制出来自己挑需要的代码拿过来改一下. NX9+VS2012 #include <NXOpen/Part.hxx> #incl ...

  8. 关于a[::-1]

    b = a[i:j]   表示复制a[i]到a[j-1],以生成新的list对象,a[:]就相当于完整复制一份a b = a[i:j:s]表示:i,j与上面的一样,但s表示步进,缺省为1.即从i到j每 ...

  9. <读书笔记>如何入门爬虫?

    大部分爬虫框架都是 发送请求 获得页面 解析页面 下载内容 存储内容 定个宏伟目标 淘宝1000页 知乎 豆瓣 ... python基础 list.dict:序列化爬取的内容 切片:分割爬取内容,获取 ...

  10. Oracle SQL外连接

    SQL提供了多种类型的连接方式,它们之间的区别在于:从相互交叠的不同数据集合中选择用于连接的行时所采用的方法不同.连接类型        定义内连接           只连接匹配的行左外连接     ...