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. 剑指offer(13)

    题目: 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) 思路: 判断当前两个根结点是否相等,如果相等,判断左右子树是否相等,如果不依次判断左右子树是否满足上 ...

  2. Storm原理

    zookeeper是对称结构

  3. AI算法第一天【概述与数学初步】

    1. 机器学习的定义: 机器从数据中学习出规律和模式,以应用在新数据上作出预测的任务 2.学习现象: (1)语言文字的认知识别 (2)图像,场景,物体的认知和识别 (3)规则:下雨天要带雨伞 (4)复 ...

  4. b2b

    sku(Stock Keeping Unit)(件)最小库存量单位. spu(一款) 一种详细的规格参数有时候跟spu,规格参数加上颜色+尺寸什么的唯一确定了一个手机,对应的就是sku spu:一款产 ...

  5. sql行转列实例

    select gh ,xm , max(A.bz) as bz , max(A.jcz) as jcz , max(A.dl) as dl , max(A.czzx) as czzx , max(A. ...

  6. ADO.NET工具类(二)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  7. josn的格式化

    public String formatJson(Object obj) { com.alibaba.fastjson.JSONObject json=(com.alibaba.fastjson.JS ...

  8. 避免MQ消息重发的简单实现思路

    一.MQ消息发送 一.MQ消息发送 1.发送端MQ-client(消息生产者:Producer)将消息发送给MQ-server: 2.MQ-server将消息落地: 3.MQ-server回ACK给M ...

  9. 洛谷-p2764(最小路径覆盖)(网络流24题)

    #include<iostream> #include<algorithm> #include<queue> #include<cstring> #in ...

  10. nginx POSTREAD阶段模块

    L:50 Realip模块 需要将--with-http_realip_model 编译进Nginx 因为nginx有可能有反向代理 获取到的客户端ip就不是原用户IP了 X-Forwarded-Fo ...