[二叉树建树&完全二叉树判断] 1110. Complete Binary Tree (25)
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)的更多相关文章
- 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 ...
- 1110. Complete Binary Tree (25)
Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...
- 1110 Complete Binary Tree (25 分)
Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...
- PAT甲题题解-1110. Complete Binary Tree (25)-(判断是否为完全二叉树)
题意:判断一个节点为n的二叉树是否为完全二叉树.Yes输出完全二叉树的最后一个节点,No输出根节点. 建树,然后分别将该树与节点树为n的二叉树相比较,统计对应的节点个数,如果为n,则为完全二叉树,否则 ...
- PAT (Advanced Level) 1110. Complete Binary Tree (25)
判断一棵二叉树是否完全二叉树. #include<cstdio> #include<cstring> #include<cmath> #include<vec ...
- 【PAT甲级】1110 Complete Binary Tree (25分)
题意: 输入一个正整数N(<=20),代表结点个数(0~N-1),接着输入N行每行包括每个结点的左右子结点,'-'表示无该子结点,输出是否是一颗完全二叉树,是的话输出最后一个子结点否则输出根节点 ...
- PAT甲级——1110 Complete Binary Tree (完全二叉树)
此文章同步发布在CSDN上:https://blog.csdn.net/weixin_44385565/article/details/90317830 1110 Complete Binary ...
- 1110 Complete Binary Tree
1110 Complete Binary Tree (25)(25 分) Given a tree, you are supposed to tell if it is a complete bina ...
- 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 ...
随机推荐
- Kafka内核理解:消息的收集/消费机制
原文:https://www.cnblogs.com/daochong/p/6425762.html 一.Kafka数据收集机制 Kafka集群中由producer负责数据的产生,并发送到对应的Top ...
- 深度学习与计算机视觉(11)_基于deep learning的快速图像检索系统
深度学习与计算机视觉(11)_基于deep learning的快速图像检索系统 作者:寒小阳 时间:2016年3月. 出处:http://blog.csdn.net/han_xiaoyang/arti ...
- CF248E Piglet's Birthday
题面 题意翻译 给定$n$个货架,初始时每个上面有$a[i]$个蜜罐. 有$q$次操作,每次操作形如$u,v,k$,表示从货架$u$上任意选择$k$个蜜罐试吃(吃过的也还能吃),吃完后把这$k$个蜜罐 ...
- 3235: [Ahoi2013]好方的蛇
3235: [Ahoi2013]好方的蛇 链接 分析: 可以求出以每个点为顶点的满足条件的矩形有多少个,单调栈求.设为sum. 然后对这个数组进行二维前缀和,可以求出每个矩阵内,以右下角.左下角为端点 ...
- 扩展gcd算法
扩展gcd算法 神tm ×度搜索exgcd 打到exg的时候出来ex咖喱棒... 球方程\(ax+by=\gcd(a,b)\)的一个解 如果\(b=0\),那么\(\gcd(a,b)=a\),取\(x ...
- C#简单的四位纯数字验证码
验证码练手,整型.四位验证码 大体意思就是:四位纯数字验证,只要验证不成功就无限验证 刚开始在纠结怎么让整个过程循环起来,什么循环放到最外层,其实就是一个循环,看来自己的循环练习的还是不够多,不够灵活 ...
- 解决在控制层springmvc框架发出的400状态的错误
错误场景: 错误分析: 这也是我第一次遇到这个类型的异常,400响应状态代表:客户端发出的请求中携带的参数与服务器端接受的参数类型不匹配,进一步就是说我后台的实体类中数据类型为Date,而前台传递过来 ...
- Apache和Nginx比较
Apache和Nginx对比 功能对比 Nginx和Apache一样,都是HTTP服务器软件,在功能实现上都采用模块化结构设计,都支持通用的语言接口,如PHP.Perl.Python等,同时还支持正向 ...
- Assert.notNull(sessionUser);
rg.springframework.util.Assert Assert翻译为中文为"断言".就是断定某一个实际的值就为自己预期想得到的,如果不一样就抛出异常.
- 【日常训练】Help Victoria the Wise(Codeforces 99C)
题意与分析 这题意思是这样的:在正方体的六面镶嵌给定颜色的宝石(相同颜色不区分),然后问最多有几种彼此不等价(即各种旋转过后看起来一致)的方案. 其实可以乱搞,因为范围只有720.求出全排列,然后每个 ...