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. 从 Spring Cloud 开始,聊聊微服务架构实践之路

    [编者的话]随着公司业务量的飞速发展,平台面临的挑战已经远远大于业务,需求量不断增加,技术人员数量增加,面临的复杂度也大大增加.在这个背景下,平台的技术架构也完成了从传统的单体应用到微服务化的演进. ...

  2. cors实现跨域(.net和jquery)

    本文引用自:http://blog.csdn.net/xuwei_xuwei/article/details/29845865 客户端 一个jquery cors请求例子: $.ajax({     ...

  3. QQ钱包,微信,京东钱包,百度钱包,支付宝AGENT

    微信Mozilla/5.0 (Linux; Android 7.0; LON-AL00 Build/HUAWEILON-AL00; wv) AppleWebKit/537.36 (KHTML, lik ...

  4. 列出远程git的全部分支

    列出全部分支 git ls-remote --heads your.git | awk 'sub(/refs\/heads\//,""){print $2}' 其中awk中sub替 ...

  5. iOS自动化探索(二)WDA API的使用

    前面我们已经安装好了WebdriverAgent, 现在可以用Facebook官方提供的API来进行一些操作 WDA API官方页面: https://github.com/facebook/WebD ...

  6. java jprofile

    java -agentpath:/opt/jprofiler8/bin/linux-x64/libjprofilerti.so=port=8849,nowait -Xdebug -Xrunjdwp:t ...

  7. 自定义音频条形图--p52

    package com.zzw.qunyingzhuan2; import android.content.Context; import android.graphics.Canvas; impor ...

  8. LeetCode OJ:Remove Nth Node From End of List(倒序移除List中的元素)

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  9. LeetCode OJ:Combination Sum (组合之和)

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  10. CSS样式:覆盖规则

    规则一:由于继承而发生样式冲突时,最近祖先获胜. CSS的继承机制使得元素可以从包含它的祖先元素中继承样式,考虑下面这种情况: <html> <head> <title& ...