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
 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
typedef struct{
int lchild, rchild;
}node;
node tree[];
int str2num(char ss[]){
int len = strlen(ss);
if(ss[] == '-')
return -;
int P = , ans = ;
for(int i = len - ; i >= ; i--){
ans += (ss[i] - '') * P;
P *= ;
}
return ans;
}
int N, lastNode, root, notRoot[] = {}, cnt = , tag = ;
void levelOrder(int root){
queue<int> Q;
Q.push(root);
while(Q.empty() == false){
int temp = Q.front();
lastNode = temp;
Q.pop();
cnt++;
if(cnt < N / && (tree[temp].lchild == - || tree[temp].rchild == -)){
tag = ;
return;
}else if(cnt == N / && (tree[temp].lchild == - && tree[temp].rchild == - || tree[temp].lchild == - && tree[temp].rchild != -)){
tag = ;
return;
}else if(cnt > N / && (tree[temp].lchild != - || tree[temp].rchild != -)){
tag = ;
return;
}
if(tree[temp].lchild != -)
Q.push(tree[temp].lchild);
if(tree[temp].rchild != -)
Q.push(tree[temp].rchild);
}
}
int main(){
scanf("%d", &N);
char str[];
for(int i = ; i < N; i++){
scanf("%s", str);
tree[i].lchild = str2num(str);
scanf("%s", str);
tree[i].rchild = str2num(str);
if(tree[i].lchild != -)
notRoot[tree[i].lchild] = ;
if(tree[i].rchild != -)
notRoot[tree[i].rchild] = ;
}
for(int i = ; i < N; i++){
if(notRoot[i] == ){
root = i;
break;
}
}
levelOrder(root);
if(tag == )
printf("NO %d", root);
else printf("YES %d", lastNode);
cin >> N;
return ;
}

总结:

1、题目要求判断二叉树是否是完全二叉树。我的办法是二叉树的层序遍历,访问一个节点就cnt++。访问前 N/2 - 1个节点时要求必须都有左右孩子。第N/2个节点要求必须是左右都非空或左非空右为空。N/2之后的节点要求左右子树都必须空。

2、网上看到还有更简单的方法,就是在层序遍历的时候把为-1的空节点也都加入队列。当访问时,遇到-1节点则查看cnt,如果cnt<N 则说明不是完全二叉树。另外注意,最后一个非空节点不一定是N-1。因为虽然是完全二叉树,但它的层次遍历节点序号不是按照0、1、2、3的顺序。

A1110. Complete Binary Tree的更多相关文章

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

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

  3. PAT_A1110#Complete Binary Tree

    Source: PAT A1110 Complete Binary Tree (25 分) Description: Given a tree, you are supposed to tell if ...

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

  5. PAT1110:Complete Binary Tree

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

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

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

  7. PAT 甲级 1110 Complete Binary Tree

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

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

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

随机推荐

  1. docker 操作镜像的基本操作

    以安装mysql为例 1.拉取镜像 docker pull mysql 错误的启动 [root@localhost ~]# docker run --name mysql01 -d mysql 42f ...

  2. windos安装maven

    1.下载好maven压缩包,并解压到相应位置,本次安装在D: 2.配置环境变量 MAVEN_HOME=D:\apache-maven-3.0.5 path=%MAVEN_HOME% 3.生成maven ...

  3. Prism框架研究(一)

    从今天起开始写一个Prism框架的学习博客,今天是第一篇,所以从最基本的一些概念开始学习这个基于MVVM的框架的学习,首先看一下Prism代表什么,这里引用一下比较官方的英文解释来看一下:Prism ...

  4. table index & delete array item

    table index & delete array item https://www.iviewui.com/components/table#ZDYLMB 编辑 row = { " ...

  5. QTP 自动化测试--点滴 等待

    1 使用wait()语句:wait(10) 等待10秒后继续执行 Window("驷惠WIN系列[汽车4S连锁管理软件] 6.").Window("应付帐款明细查询&qu ...

  6. vue事件綁定

    事件綁定可以是一個句子,一個函數名稱,也可以是一個函數. 事件修飾符,按鍵修飾符.

  7. Lodop输出页面input文本框的最新值

    默认使用Lodop打印页面上的文本框等,会发现虽然页面上文本框输入了值,打印预览却是空的,这是由于没有把最新的值传入Lodop. 如图,演示的是Lodop如何输出文本框内的新值,这里整个页面只有inp ...

  8. centos7优化启动项,关闭一些不必要开启的服务

    CentOS7已不再使用chkconfig 管理启动项 使用 systemctl list-unit-files 可以查看启动项 systemctl list-unit-files | grep en ...

  9. Promise实现队列

    有时候我不希望所有动作一起发生,而是按照一定顺序,逐个进行 var promise=doSomething(); promise=promise.then(doSomethingElse); prom ...

  10. MySQL官方教程及各平台的安装教程和配置详解入口

    官方文档入口: https://dev.mysql.com/doc/ 一般选择MySQL服务器版本入口: https://dev.mysql.com/doc/refman/en/ 在右侧有版本选择: ...