[二叉树建树&完全二叉树判断] 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 ...
随机推荐
- Golint的简易使用方法
根据作者的说法: Golint is a linter for Go source code. Golint differs from gofmt. Gofmt reformats Go source ...
- LaTeX自定义宏包、类文件的默认搜索路径设置方法
对于自定义的LaTeX宏包与类,在调用时可以通过在命令\documentclass{}与\usepackage{}命令中指定完整路径或者相对路径,这样确实可以调用,但是编译时总是有烦人的警告信息, ...
- 准确率,召回率,F值
下面简单列举几种常用的推荐系统评测指标: 1.准确率与召回率(Precision & Recall) 准确率和召回率是广泛用于信息检索和统计学分类领域的两个度量值,用来评价结果的质量.其中精度 ...
- 洛谷 P1350 车的放置
洛谷 P1350 车的放置 题目描述 有下面这样的一个网格棋盘,a,b,c,d表示了对应边长度,也就是对应格子数. 当a=b=c=d=2时,对应下面这样一个棋盘 要在这个棋盘上放K个相互不攻击的车,也 ...
- 19-[JavaScript]-DOM
1.DOM操作 在JS中,所有的事物都是节点,元素.文本等都是节点. 应用场景:可以通过节点进行DOM对象的增删改查 (1)获取DOM节点的方法 //通过id获取,唯一的 var oDiv = doc ...
- 2_C语言中的数据类型 (一)2.1.常量和字符串常量
2.1 常量就是在程序中不可变化的量,常量在定义的时候必须给一个初值. 1.1.1 #define 定义一个宏常量 1.1.2 const 定义一个const常量 ...
- Atcoder2167 Blackout
Atcoder2167 Blackout zjoi讲过的一道神题啊... 首先把每个黑点(a,b)看成一条有向边a->b,然后这个图就变成了一张有自环的有向图. 然后弱联通块就分开了,对于每个连 ...
- setjmp/longjmp 处理异常
#include <stdio.h> #include <stdlib.h> #include <setjmp.h> jmp_buf jb; void f1() { ...
- 详解YUV420数据格式
原文地址:http://www.cnblogs.com/azraelly/archive/2013/01/01/2841269.html 1. YUV简介 YUV定义:分为三个分量,“Y”表示明亮度( ...
- Unity 实现一个简单的 TPS 相机
效果如下: 代码如下: public class TPSCamera : MonoBehaviour { /// <summary> /// 目标对象 /// </summary&g ...