PAT1110:Complete Binary Tree
1110. 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 (<=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的更多相关文章
- [Swift]LeetCode919. 完全二叉树插入器 | Complete Binary Tree Inserter
A complete binary tree is a binary tree in which every level, except possibly the last, is completel ...
- A1110. Complete Binary Tree
Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...
- 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 甲级 1110 Complete Binary Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805359372255232 Given a tree, you are ...
- 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 ...
- [二叉树建树&完全二叉树判断] 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 1110 Complete Binary Tree[判断完全二叉树]
1110 Complete Binary Tree(25 分) Given a tree, you are supposed to tell if it is a complete binary tr ...
- 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 ...
- 1110 Complete Binary Tree
1110 Complete Binary Tree (25)(25 分) Given a tree, you are supposed to tell if it is a complete bina ...
随机推荐
- AndroidBinder进程间通信系统-android学习之旅(86)
目录 前言及知识准备 Service组件结构 Clinet组件结构 与Binder驱动程序交互 总结 Binder进程间通信实例 问题 本次主要介绍Android平台下Binder进程间通信库.所谓通 ...
- 解决ADT大量出现"Unexpected value from nativeGetEnabledTags: 0"的问题
在过滤器中输入下面的代码即可:(此方法搜索于网略) ^(?!.*(nativeGetEnabledTags)).*$
- Gradle 1.12用户指南翻译——第二十二章. 标准的 Gradle 插件
其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Github上的地址: https://g ...
- Android BLE与终端通信(四)——实现服务器与客户端即时通讯功能
Android BLE与终端通信(四)--实现服务器与客户端即时通讯功能 前面几篇一直在讲一些基础,其实说实话,蓝牙主要为多的还是一些概念性的东西,当你把概念都熟悉了之后,你会很简单的就可以实现一些逻 ...
- django-debug-tools 使用
用django开发很快也很容易,但是很多时候我们的经验并不是很足,就会给自己挖下很多坑,不管是性能问题,还是开发语言使用技巧问题都会给应用的稳定带来危害, 开发之后的调试和调优就显得很重要,今天就尝试 ...
- C语言之可变参实现scanf函数
既然有printf函数可变参实现,那就一定有scanf函数的可变参实现.废话不多说,源码奉上: 本源码不过多分析,如要明白原理,请翻本博客以往的文章看说明. 欢迎关注新浪微博:http://weibo ...
- PS 滤镜——旋转模糊
这里给出灰度图像的模糊算法,彩色图像只要分别对三个通道做模糊即可. %% spin blur % 旋转模糊 clc; clear all; close all; I=imread('4.jpg'); ...
- 数据结构-C语言递归实现树的前中后序遍历
#include <stdio.h> #include <stdlib.h> typedef struct tree { int number ; struct tree *l ...
- 面试之路(18)-java的函数参数传递类型之值传递还是引用传递
关于这个问题争论了很久,最近也是偶然发现这个问题 经典名言: O'Reilly's Java in a Nutshell by David Flanagan (see Resources) puts ...
- 关于Html5发展和应用前景
现在的HTML5就像当年崭露头角时的Ajax,有人在做,但不知道叫它什么.最近,苹果在 HTML5上大做文章,而著名的Web设计师Eric Meyer则提出了Web Stacks的概念.Alex Ke ...