1110. Complete Binary Tree (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 (<=20) 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

思路
判断一棵二叉树是不是完全二叉树。 层次遍历二叉树,当遍历到"-"节点时(表示空节点),检查树的节点数N和遍历次数cnt是否相等,相等yes,不想等no。
注意:输入用string而不用char,因为char不能表示两位数以上的数字。之前没注意导致代码只有部分ac。 代码

 

#include<iostream>
#include<vector>
#include<queue>
using namespace std; class Node
{
public:
int left;
int right;
};
int main()
{
int N;
while(cin >> N)
{
vector<Node> nodes(N);
vector<bool> isroot(N,true);
//Build tree
for(int i = 0;i < N;i++)
{
string left,right;
cin >> left >> right;
if(left == "-")
nodes[i].left = -1;
else
{
nodes[i].left = stoi(left);
isroot[nodes[i].left] = false;
}
if(right == "-")
nodes[i].right = -1;
else
{
nodes[i].right = stoi(right);
isroot[nodes[i].right] = false;
}
}
//Find root
int root = -1;
for(int i = 0;i < isroot.size();i++)
{
if(isroot[i])
{
root = i;
break;
}
}
//BFS
queue<int> q;
q.push(root);
int cnt = 0,lastindex = -1;
while(!q.empty())
{
int tmp = q.front();
q.pop();
if(tmp == -1)
{
break;
}
cnt++;
lastindex = tmp;
q.push(nodes[tmp].left);
q.push(nodes[tmp].right);
}
//Output
if(cnt == N)
cout << "YES" << " " << lastindex <<endl;
else
cout << "NO" << " " << root << endl;
}
}

  

PAT1110:Complete Binary Tree的更多相关文章

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

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

  2. A1110. Complete Binary Tree

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

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

  4. PAT 甲级 1110 Complete Binary Tree

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

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

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

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

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

  9. 1110 Complete Binary Tree

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

随机推荐

  1. STL - 各个容器的使用时机

    deque的使用场景:比如排队购票系统,对排队者的存储可以采用deque,支持头端的快速移除,尾端的快速添加.如果采用vector,则头端移除时,会移动大量的数据,速度慢. vector与deque的 ...

  2. XWork容器的存储结构

    我们可以看到,在Container的默认实现,ContainerImpl中有两个实例变量.factoris和factoryNamesByType. 对象制造工厂 class ContainerImpl ...

  3. Linux内核中SPI总线驱动分析

    本文主要有两个大的模块:一个是SPI总线驱动的分析 (研究了具体实现的过程): 另一个是SPI总线驱动的编写(不用研究具体的实现过程). 1 SPI概述 SPI是英语Serial Peripheral ...

  4. Mongodb系列之--mongodb的启动与关闭

    Mongodb的开启   默认启动:   $ ./mongodb   默认数据保存路径:/data/db/ 默认端口:27017   修改默认路径:   --dbpath $ ./mongdb --d ...

  5. Java-HttpServletRequest

    //继承了ServletRequest接口,给servlet提供Request请求信息,servlet 容器会创建以后HttpServletRequest对象 //并把它作为一个参数给service函 ...

  6. PS 滤镜算法原理——碎片效果

    %%% Fragment %%% 对原图做四个方向的平移,然后对平移的结果取平均 %%% 碎片效果 clc; clear all; Image=imread('4.jpg'); Image=doubl ...

  7. SharePoint 2013 页面访问,Url中间多一段&quot;_layouts/15/start.aspx#&quot;

    问题描述: 我想访问如下页面 http://Host/_layouts/15/ManageFeatures.aspx 点击以后页面地址没有错,但是中间多了一段"_layouts/15/sta ...

  8. asp.net 调试与iis部署的问题

    第一个问题:编译器错误信息: CS0016: 未能写入输出文件"c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET ...

  9. Java内存模型_重排序

    重排序:是指编译器和处理器为了优化程序性能而对指令序列进行重新排序的一种手段 1..编译器优化的重排序.编译器在不改变单线程程序语义的前提下,可以重新安排语句的执行顺序. 2..指令级并行的重排序.现 ...

  10. 一个基础的for循环面试题

    下面的这段程序主要考察的就是for循环的基础,输出什么?????? [html] view plaincopyprint? public class test { /** * @param args ...