1110. Complete Binary Tree (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

分析:这道题目的建树与以前的通过中序和前序或者中序和后序建树不同,这里给出了每个孩子的左右孩子,根据这些信息来建树。做法是申明一个节点数组,将对应的节点信息填入数组即可,而左右孩子指针就是数组的下标。另外,关于完全二叉树的判断,网上也有很多资料。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std; struct Node
{
int data;
int lchild,rchild;
}node[]; int inDreeg[]={}; bool judge(int root,int & last_node)
{
if(root==-) return true;
queue<int> q;
q.push(root);
int flag=;
int now;
while(!q.empty())
{
now=q.front();
q.pop();
//cout<<node[now].data<<endl;
if(flag==)
{
if(node[now].lchild!=-&&node[now].rchild!=-) flag=;
else if(node[now].lchild==-&&node[now].rchild!=-) return false;
else flag=;
}
else
{
if(node[now].lchild!=-||node[now].rchild!=-) return false;
}
if(node[now].lchild!=-) q.push(node[now].lchild);
if(node[now].rchild!=-) q.push(node[now].rchild);
}
last_node=node[now].data;
return true;
} int main()
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
{
string l,r;
cin>>l>>r;
node[i].data=i;
if(l=="-")
{
node[i].lchild=-;
}
else
{
int id=atoi(&l[]);
node[i].lchild=id;
inDreeg[id]+=;
} if(r=="-")
{
node[i].rchild=-;
}
else
{
int id=atoi(&r[]);
node[i].rchild=id;
inDreeg[id]+=;
}
}
bool ans;
int root=-;
for(int i=;i<n;i++)
{
if(inDreeg[i]==)
{
root=i;
break;
}
}
int last;
ans=judge(root,last);
if(ans==true)
{
printf("YES %d\n",last);
}
else
{
printf("NO %d\n",root);
}
}

[二叉树建树&完全二叉树判断] 1110. Complete Binary Tree (25)的更多相关文章

  1. PAT Advanced 1110 Complete Binary Tree (25) [完全⼆叉树]

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

  2. 1110. Complete Binary Tree (25)

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

  3. 1110 Complete Binary Tree (25 分)

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

  4. PAT甲题题解-1110. Complete Binary Tree (25)-(判断是否为完全二叉树)

    题意:判断一个节点为n的二叉树是否为完全二叉树.Yes输出完全二叉树的最后一个节点,No输出根节点. 建树,然后分别将该树与节点树为n的二叉树相比较,统计对应的节点个数,如果为n,则为完全二叉树,否则 ...

  5. PAT (Advanced Level) 1110. Complete Binary Tree (25)

    判断一棵二叉树是否完全二叉树. #include<cstdio> #include<cstring> #include<cmath> #include<vec ...

  6. 【PAT甲级】1110 Complete Binary Tree (25分)

    题意: 输入一个正整数N(<=20),代表结点个数(0~N-1),接着输入N行每行包括每个结点的左右子结点,'-'表示无该子结点,输出是否是一颗完全二叉树,是的话输出最后一个子结点否则输出根节点 ...

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

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

  8. 1110 Complete Binary Tree

    1110 Complete Binary Tree (25)(25 分) Given a tree, you are supposed to tell if it is a complete bina ...

  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. python3爬虫-下载网易云音乐,评论

    # -*- coding: utf-8 -*- ''' 16位随机字符的字符串 参数一 获取歌曲下载地址 "{"ids":"[1361348080]" ...

  2. Mysql linux 安装文档

    1.安装依赖包 yum -y install gcc-c++ ncurses-devel cmake make perl gcc autoconf automake zlib libxml libgc ...

  3. 20145234黄斐《Java程序设计》第十周

    网络编程 网络概述 概述 网络编程技术是当前一种主流的编程技术,随着联网趋势的逐步增强以及网络应用程序的大量出现,所以在实际的开发中网络编程技术获得了大量的使用. 计算机网络概述 IP地址: 为了能够 ...

  4. SQL Server 查询请求

    当SQL Server 引擎接收到用户发出的查询请求时,SQL Server执行优化器将查询请求(Request)和Task绑定,并为Task分配一个Workder,SQL Server申请操作系统的 ...

  5. (转)js数组与字符串的相互转换方法

    一.数组转字符串 需要将数组元素用某个字符连接成字符串,示例代码如下: var a, b; a = new Array(0,1,2,3,4); b = a.join("-"); 二 ...

  6. 总结hibernate框架的常用检索方式

    1.hibernate框架的检索方式有以下几种: OID检索:根据唯一标识OID检索数据 对象导航检索:根据某个对象导航查询与该对象关联的对象数据 HQL检索:通过query接口对象查询 QBC检索: ...

  7. 分享一个DataTable转List强类型的类库

    类库扩展自Datatable,可以直接用Datatable.ToList<T>()进行转换.为了方便把DataReader装入Datatable,开扩展了一个LoadForReader(t ...

  8. Netty源码分析第2章(NioEventLoop)---->第3节: 初始化线程选择器

    Netty源码分析第二章:NioEventLoop   第三节:初始化线程选择器 回到上一小节的MultithreadEventExecutorGroup类的构造方法: protected Multi ...

  9. Gitlab CI-3.遇到的问题

    五.遇到的问题 1. cannot validate certificate for x.x.x.x because it doesn't contain any IP SANs 报错信息:ERROR ...

  10. GlusterFS分布式存储集群-2. 使用

    参考文档: Quick Start Guide:http://gluster.readthedocs.io/en/latest/Quick-Start-Guide/Quickstart/ Instal ...