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. webpack初始化

    1. 安装node js 2. 安装npm 3. 在桌面新建一个文件夹 4.利用cmd 进入文件夹 5.在cmd中创建一个新文件夹并且进入 6.npm init -y  生成page.json 7. ...

  2. react css拓展 使用less

    react 之中使用less 其实质只需要看一下resct 使用css的配置项,就能明白个大概了  第一步   还是下载 npm i  less less-loader -save 下载less 和 ...

  3. 【FJWC2018】最大真因数

    题面 Description 一个合数的真因数是指这个数不包括其本身的所有因数, 例如 6 的正因数有1, 2, 3, 6,其中真因数有 1, 2, 3. 一个合数的最大真因数则是这个数的所有真因数中 ...

  4. JS实现菜单滚动到一定高度后固定

    在有些网页中我们会发现会有这样的现象:某个div会随着屏幕的滚动达到一定高度的时候位置就固定下来了.例如一下导航条: 那么这里就需要用到JS的逻辑方法来实现了. html <div id=&qu ...

  5. Linux如何删除特殊字符文件名或目录?

    通过文件的inode号删除文件 先用ls -i 找出要删除文件的inode 号 2ls -i |grep xxxxxx|awk '{print $2}'|xargs -i rm -f {} xxxxx ...

  6. delphi 获取本机IP地址和MAC地址 (转)

    unit NetFunc; interface uses SysUtils, Windows, dialogs, winsock, Classes, ComObj, WinInet, Variants ...

  7. NX二次开发-UFUN写入本地文本文档uc4524

    1 NX9+VS2012 2 3 #include <uf.h> 4 #include <uf_cfi.h> 5 #include <uf_ui.h> 6 7 us ...

  8. NX二次开发-UFUN拾取屏幕位置UF_UI_specify_screen_position

    #include <uf.h> #include <uf_ui.h> UF_initialize(); //拾取屏幕位置 //在屏幕用鼠标拾取一点 char sMessage[ ...

  9. 解决:Map的area属性标签鼠标Hover可以给area加背景

    css的area标签是不支持hover的,只有a标签才支持.li标签在IE浏览器下才支持,所以采用jquery的mouseenter和mouseleave事件完成.首先讲jQuery对应的事件:1.m ...

  10. 23-Ubuntu-文件和目录命令-查看文件内容-more 和 cat

    1. more---适合查看内容多的文件 格式: more 文件名 操作键 功能 空格键 显示手册页下一屏 Enter键 一次滚动手册页的一行 b 返回上一屏 f 前滚一屏 q 退出 例: more ...