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. 本地上传项目到github

    https://www.cnblogs.com/rosej/p/6056467.html(copy)

  2. WPF当属性值改变时利用PropertyChanged事件来加载动画

    在我们的程序中,有时我们需要当绑定到UI界面上的属性值发生变化从而引起数据更新的时候能够加载一些动画,从而使数据更新的效果更佳绚丽,在我们的程序中尽量将动画作为一种资源放在xaml中,而不是在后台中通 ...

  3. Fiddler 学习笔记---命令、断点

    输入命令框: 1 输入 ?51testing  高亮显示对应记录 2 >10 选择body大于10的记录 3 <10 选择body<10的记录 4 =200 选择result=200 ...

  4. css繼承

    概念:就是上級設置了css屬性,下級默認有同樣的樣式,如果下級需要不一樣的樣式,那麼就需要單獨對下級設置. 範圍: font-family:字體 font-weight:粗細 font-size:尺寸 ...

  5. php new self()

    php里new self() 一般在类内部使用,作用是对自身类实例化 <?php class test{ public function __construct(){        echo ' ...

  6. 转载:关于JESD204B转换器与FPGA匹配的设计关键点

    http://www.dzsc.com/data/2014-11-27/107442.html 随着更多的模数转换器(ADC)和数模转换器(DAC)支持最新的JESD204B串行接口标准,出现了FPG ...

  7. 了解C#中的HashSet与示例

    在C#中引入HashSet 在.NET框架中,有几个类可用于执行这些操作.一些课程如下: 列表 字典 哈希集 队列 集合 在C#编程中,像ArrayList,List这样的集合,只需添加其中的值,而不 ...

  8. OfficeToHtmlHelper

    public class Office2HtmlHelper { /// <summary> /// Word转成Html /// </summary> /// <par ...

  9. 基于opencv图片切割

    基于opencv图片切割为n个3*3区块 工作原因,切割图片,任务急,暂留调通的源码,留以后用. package com.rosetta.image.test; import org.opencv.c ...

  10. VMware ezmomi工具使用

    用两个静态IP克隆模板: ezmomi clone --template centos67 --hostname test01 --cpus 2 --mem 4 --ips 172.10.16.203 ...