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. linux的一些操作

    在终端输入cat /etc/issue 查看ubuntu的半磅不知道ubuntu特权用户root的密码时:Ubuntu在默认情况下是不启用root用户的,所以这对于一下对于linux命令不熟悉的用户在 ...

  2. Spring mvc使用不了jstl 或者 Spring mvc不解析jstl

    最近我搭了一个maven的springMVC的项目发现前端怎么也识别不了我的jstl,我查询了很多方法,导致这种情况的原因有很多 1.jar引用不对,maven中的正确导入可用的jar <dep ...

  3. websocket之django简单使用

    WebSocket protocol: WebSocket protocol 是HTML5一种新的协议.它是实现了浏览器与服务器全双工通信(full-duplex).HTML5定义了WebSocket ...

  4. "下载"文件夹的desktop.ini

    下载 [.ShellClassInfo] LocalizedResourceName=@%SystemRoot%\system32\shell32.dll,-21798 IconResource=%S ...

  5. 十 web爬虫讲解2—Scrapy框架爬虫—Scrapy安装—Scrapy指令

    Scrapy框架安装 1.首先,终端执行命令升级pip: python -m pip install --upgrade pip2.安装,wheel(建议网络安装) pip install wheel ...

  6. deep learning新征程(二)

    deep learning新征程(二) zoerywzhou@163.com http://www.cnblogs.com/swje/ 作者:Zhouwan  2016-4-5   声明 1)该Dee ...

  7. CSS: transitions

    CSS Transitions CSS transitions allows you to change property values smoothly (from one value to ano ...

  8. 静态嵌套类(Static Nested Class)和内部类(Inner Class)的不同?

    Static Nested Class是被声明为静态(static)的内部类,它可以不依赖于外部类实例被实例化.而通常的内部类需要在外部类实例化后才能实例化,其语法看起来挺诡异的,如下所示. /** ...

  9. C# 中字段和属性的使用时机

    在C#中,我们可以非常自由的.毫无限制的访问公有字段,但在一些场合中,我们可能希望限制只能给字段赋于某个范围的值.或是要求字段只能读或只能写,或是在改变字段时能改变对象的其他一些状态,这些单靠字段是无 ...

  10. oracle的序列号(sequence)

    oracle的自增列,要采用序列号(sequence). 初始化阶段要手动建立一个sequence,然后插入的时候,还要手动自己去读这个sequence的nextval赋给相关字段,如ID,麻烦的很. ...