Source:

PAT A1110 Complete Binary Tree (25 分)

Description:

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

Keys:

  • 完全二叉树(Complete Binary Tree)

Attention:

  • atoi(s.c_str()); 字符串转化为int,atof 对应 double,atoll 对应 long long;
  • 含非数字则截取前面的数字部分,首字符非数字则返回0

Code:

 #include<cstdio>
#include<string>
#include<queue>
#include<iostream>
using namespace std;
const int M=;
int h[M]={};
struct node
{
int left,right;
}tree[M]; bool isComplete(int root, int &last)
{
queue<int> q;
q.push(root);
while(!q.empty())
{
root = q.front();
q.pop();
if(root != -)
{
last = root;
q.push(tree[root].left);
q.push(tree[root].right);
}
else
{
while(!q.empty())
{
if(q.front() != -)
return false;
q.pop();
}
}
}
return true;
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif int n,root,last;
scanf("%d", &n);
for(int i=; i<n; i++)
{
string l,r;
cin >> l >> r;
if(l == "-")
tree[i].left=-;
else{
tree[i].left = atoi(l.c_str());
h[tree[i].left]=;
}
if(r == "-")
tree[i].right=-;
else{
tree[i].right = atoi(r.c_str());
h[tree[i].right]=;
}
}
for(int i=; i<n; i++){
if(h[i]==){
root=i;
break;
}
}
if(isComplete(root, last))
printf("YES %d", last);
else
printf("NO %d", root); return ;
}

PAT_A1110#Complete Binary Tree的更多相关文章

  1. PAT1110:Complete Binary Tree

    1110. Complete Binary Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  2. [Swift]LeetCode919. 完全二叉树插入器 | Complete Binary Tree Inserter

    A complete binary tree is a binary tree in which every level, except possibly the last, is completel ...

  3. A1110. Complete Binary Tree

    Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...

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

  5. PAT 甲级 1110 Complete Binary Tree

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

  6. 1110 Complete Binary Tree (25 分)

    1110 Complete Binary Tree (25 分) Given a tree, you are supposed to tell if it is a complete binary t ...

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

  8. PAT 1110 Complete Binary Tree[判断完全二叉树]

    1110 Complete Binary Tree(25 分) Given a tree, you are supposed to tell if it is a complete binary tr ...

  9. PAT 1110 Complete Binary Tree[比较]

    1110 Complete Binary Tree (25 分) Given a tree, you are supposed to tell if it is a complete binary t ...

随机推荐

  1. assign retain 和copy的区别

    assign 对基础数据类型 (NSInteger,CGFloat)和C数据类型(int, float, double, char)等 等. 此标记说明设置器直接进⾏行赋值,这也是默认值.在使⽤用垃圾 ...

  2. Spring MVC-处理程序映射(Handler Mapping)-控制器类名称处理程序映射(Controller Class Name Handler Mapping)示例(转载实践)

    以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_controllerclassnamehandlermapping.htm 说明: ...

  3. 查看编译器的默认include 路径

    echo | gcc -v -x c++ -E - echo | g++ -v -x c++ -E - `gcc -print-prog-name=cc1plus` -v `g++ -print-pr ...

  4. ubuntu系统启动qtceator时提示:Qt5.5.1/Tools/QtCreator/lib/qtcreator/plugins/libHelp.so: 无法加载库

    在ubuntu系统下安装好qt5.5后启动qtceator时提示:Qt5.5.1/Tools/QtCreator/lib/qtcreator/plugins/libHelp.so: 无法加载库Qt5. ...

  5. QMessageBox 的四种用法

    void MainWindow::on_info_clicked() { //info QMessageBox::information(this, "Title", " ...

  6. Android Calendar的运用

    import java.text.DateFormat; import java.text.ParsePosition; import java.text.SimpleDateFormat; impo ...

  7. python pickle to json

    ref: https://gist.github.com/Samurais/567ebca0f59c612eb977065008aad867 ''' Convert a pkl file into j ...

  8. 78.员工个人信息保镖页面 Extjs 页面

    1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8" ...

  9. win7下安装memcache

    Windows7 x64在Wamp集成环境下安装Memcache,步骤如下: 1.Memcached-win64 下载 (1)最新版本下载:http://blog.couchbase.com/memc ...

  10. POJ 1160 DP

    题目: poj 1160 题意: 给你n个村庄和它的坐标,现在要在其中一些村庄建m个邮局,想要村庄到最近的邮局距离之和最近. 分析: 这道题.很经典的dp dp[i][j]表示建第i个邮局,覆盖到第j ...