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. K3 WISE安全认证方式

    k/3中间层注册三种安全认证方式: 交互式用户方式,网络服务方式,信任方式,是指组件服务中生成的COM+应用程序中的组件包的运行账户(注册中间层后产生很多ebo开头的和kdsvrmgr组件包). 三种 ...

  2. C# Note16: wpf window 中添加enter和双击事件

     一.添加回车(enter)事件 在C#编程时,有时希望通过按回车键,控件焦点就会自动从一个控件跳转到下一个控件进行操作. 以用户登录为例,当输入完用户名和密码后, 需要点击登录按钮,而登录按钮必须获 ...

  3. Django--cookie 和 session

    一 . 会话跟踪 简单来说就是如果谢霆锋登录了淘宝,然后他点击淘宝内的任何连接就不需要在登录了,这是由于这个会话中数据共享, 也就是会话跟踪, 我们都知道http协议是短连接 无状态的,也就是说你登录 ...

  4. python之路--MySQL权限管理 数据备份还原

    一 权限管理 mysql最高管理者是root用户, 这个一般掌握在公司DBA手里, 当你想去对数据库进行一些操作的时候,需要DBA授权给你. 1. 对新用户增删改 1. 创建用户 # 要先use my ...

  5. linux audit审计(8)--ausearch搜索audit日志文件

    ausearch这个工具,可以针对指定的事件来搜索audit日志文件.默认情况下,ausearch搜索/var/log/audit/audit.log这个文件. The ausearch utilit ...

  6. delphi怎样在关闭程序时弹出窗口?

    我想在关闭delphi编译的程序时,弹出“您是否确实要退出的窗口”点击否不退出,是退出 在主窗体的CloseQuery事件里,使用messagebox进行提示,根据选择的按钮对Canclose进行设置 ...

  7. MySQL——基础操作

    参考博客:http://www.cnblogs.com/wupeiqi/articles/5713315.html 1.创建用户.授权(默认root,密码为空) 创建: create user 'al ...

  8. How to enable AHCI on Windows7

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\msahci

  9. 12.k8s的存储卷创建过程

    数据持久化需要数据卷.kubernetes生态提供海量的存储驱动和存储使用方式. [root@master song]# cat pod-demo.yml apiVersion: v1 kind: P ...

  10. Express学习 ------模版引擎(handlebars)

    Handlebars一款js模版引擎,我们在做客户端开发的时候,也可能已经使用过.它语法比较简单,和我们平常写的html 一样,只不过html 中可以加入handlebars 表达式. handleb ...