1110 Complete Binary Tree (25)(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
 
题意:
给出树中每个结点的孩子结点(若无孩子,用-表示),要求判断其是否为完全二叉树。若是,输出YES及最后一个结点;若不是,输出NO和根结点。
 
思路:
利用层序遍历,把树的所有结点(包括空结点)都push进队列。设立全局变量cnt,初始化cnt=0,用来记录已经访问到的非空结点的个数,在遇到第一个空结点时,若cnt==n,则是完全二叉树;若cnt<n,则不是完全二叉树。
 
如下图,该图为完全二叉树,则在队列中元素的存储是这样的:
null(4r) null(4l) null(3r) null(3l) null(2r) 4 3 2 1
而对于如下这棵树,不是完全二叉树,则在队列中元素的存储是这样的:
null(4r) null(4l)  null(3l) null(2r) null(2l) 3 2 1
 
代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
using namespace std;

struct Node{
    int left,right;
}Tree[];
int n;

bool isCBT(int root,int &lastval)
{
    ;
    queue<int> q;
    q.push(root);
    while(!q.empty()){
        int top=q.front();
        q.pop();
        ){
            cnt++;
            lastval=top;
            q.push(Tree[top].left);
            q.push(Tree[top].right);
        }else {
            if(cnt==n) return true;
            else return false;
        }
    }
}

int main()
{
    scanf("%d",&n);
    ],u[];
    int left,right;
    ];
    memset(isRoot,true,sizeof(isRoot));
    ;i<n;i++){
        scanf("%s %s",v,u);
        ]==;
        else{
            left=atoi(v);
            isRoot[left]=false;
        }
        ]==;
        else{
            right=atoi(u);
            isRoot[right]=false;
        }
        Tree[i].left=left;
        Tree[i].right=right;
    }
    ;
    while(isRoot[root]==false) root++;
    ;
    bool flag=isCBT(root,lastVal);
    if(flag) printf("YES %d\n",lastVal);
    else printf("NO %d\n",root);
    ;
}

1110 Complete Binary Tree的更多相关文章

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

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

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

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

  5. PAT甲级——1110 Complete Binary Tree (完全二叉树)

    此文章同步发布在CSDN上:https://blog.csdn.net/weixin_44385565/article/details/90317830   1110 Complete Binary ...

  6. PAT 甲级 1110 Complete Binary Tree

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

  7. 1110. Complete Binary Tree (25)

    Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...

  8. PAT 1110 Complete Binary Tree

    Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...

  9. 1110 Complete Binary Tree (25 分)

    Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...

随机推荐

  1. Shell 运算符 if

    Shell 支持多种运算符,包括: 算术运算符 原生bash不支持简单的数学运算,可以使用 expr,let 关系运算符 布尔运算符 字符串运算符 文件运算符 算术运算符 包括加减乘除,取余(%).赋 ...

  2. Mac环境下Android Studio配置Git以及最基本使用

    Git是分布式版本管理工具,现在使用十分广泛,相对于SVN,GIT的使用更加方便,在离线环境下,仍然可以进行版本控制工作.且速度十分快.在Windows下,先需要自行安装Git程序,网址git-scm ...

  3. Flask 分页的简单用法 / flask_sqlalchemy /无刷新翻转页面(原创)

    flask_sqlalchemy对象提供分页方法 1. 后台views代码: from models import <table_name> #导入model的对象 @app.route( ...

  4. LeetCode OJ:Lowest Common Ancestor of a Binary Tree(最近公共祖先)

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  5. MySQL学习笔记2018-02-07更新

    前言:本人wechat:YWNlODAyMzU5MTEzMTQ=. 如果内容有错,请指出来. win10下安装 https://dev.mysql.com/downloads/mysql/下载并解压m ...

  6. canvas - 柱子效果

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  7. iOS7.1以后企业应用发布需要HTTPS协议,解决步骤

    操作系统是Window下. 第一步安装软件 1.安装Tomcat6.0.35(Tomcat7.0.34测试通过) 2.安装JDK6(1.6.0_10-rc2,其它版本没测试) 3.安装openssl ...

  8. jdk1.8 HashMap 实现 数组+链表/红黑树

    转载至 http://www.cnblogs.com/leesf456/p/5242233.html 一.前言 在分析jdk1.8后的HashMap源码时,发现网上好多分析都是基于之前的jdk,而Ja ...

  9. dojo chart详解

    Dojo提供了一套很完善的统计图(Chart)接口,在dojox/charting下面,可以支持很多种类型的. .简介 Dojo统计图提供快速的.简单的接口实现美观的.交互性强的web统计图表的实现. ...

  10. [置顶] 【机器学习PAI实践三】雾霾成因分析

    一.背景 如果要人们评选当今最受关注话题的top10榜单,雾霾一定能够入选.如今走在北京街头,随处可见带着厚厚口罩的人在埋头前行,雾霾天气不光影响了人们的出行和娱乐,对于人们的健康也有很大危害.本文通 ...