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. koa2使用&&中间件&&angular2的koa托管

    文章导航 1,koa2使用: 2,写中间件: 3,koa2路由配置angular2; 一.先上代码,一篇,看完koa2中大多基础方法: const Koa=require('koa'); const ...

  2. unity 使用MVC模式

    这两天看了下老大的项目,他基本都是用MVC模式,写的很好,在此把我理解的记录下来 Model:实体对象(对应数据库记录的类) View:视图 presenter(controller):业务处理 vi ...

  3. jquery attr与prop的区别与联系

    最近开发中发现用attr无法设置checkbox的选中事件,在网上找了下说要用prop,所以总结下两者的区别. 1.操作的对象不同 attr:操作的是HTML文档节点属性 prop:操作的是js对象属 ...

  4. 本地动态SQL

    (转自:http://blog.itpub.net/26622598/viewspace-718134) 一.什么是动态SQL 大多数PL/SQL都做着一件特殊的结果可预知的工作.例如,一个存储过程可 ...

  5. 服务器证书安装配置指南(IIS7.0)

    一.  生成证书请求 1.    进入IIS控制台   进入IIS控制台,并选择服务器的服务器证书设置选项.  2.    添加证书请求   进入服务器证书配置页面,并选择“创建证书申请”  3.   ...

  6. Python基础学习(第9天)

    第九课:动态类型 1.在Python中,有一类特殊的对象,是专门用来存储数据的,常见的有数字.字符串.数列.字典等.这些对象是存储在内存中的实体,我们并不能直接接触到它们,在程序中接触的是对象名,是指 ...

  7. 《深入理解java虚拟机》学习笔记之虚拟机即时编译详解

    郑重声明:本片博客是学习<深入理解java虚拟机>一书所记录的笔记,内容基本为书中知识. Java程序最初是通过解释器(Interpreter)进行解释执行的,当虚拟机发现某个方法或代码块 ...

  8. mysql锁之Next-Key Locks

    一个Next-key锁结合了行锁和gap锁. InnoDB执行一个行级别锁在这样的一个途径,那就是它搜索或者扫描一个表索引时,它设置共享或者独占锁在它遭遇的索引记录上.于是,行级锁是真实的行记录锁.一 ...

  9. 配置文件Struts.xml 中type属性 redirect,redirectAction,chain的区别

    1.redirect:action处理完后重定向到一个视图资源(如:jsp页面),请求参数全部丢失,action处理结果也全部丢失.  2.redirectAction:action处理完后重定向到一 ...

  10. request.getDispatcher().forward(request,response)和response.sendRedirect()的区别

    在进行web开发时,跳转是最常见的,今天在这里来学习下2种跳转: 第一种是request.getDispatcher().forward(request,response): 1.属于转发,也是服务器 ...