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 分析:判断是否是完全二叉树。先求根节点,可以设置一个数组child,如果输入过程中如果该结点被当做孩子节点那它一定不是根节点,输入完毕后从头开始遍历child数组,第一个是false的child[i]跳出,i就是根节点。那么如何判断是完全二叉树呢?
这里提供两种方法。 1、层序遍历,每次把空节点也Push进队列中,完全二叉树层序遍历过程中,如果遇到空节点了,说明一定已经遍历完非空节点;而对非完全二叉树,如果遇到空节点了,后面还必定有非空节点。于是遍历过程中用一个变量统计已入队过的节点数量。
2、递归求最大下标值,完全二叉树中,最大下标值=最大结点数(起始为1); 而非完全二叉树中,最大下标值>最大结点数
代码分别如下:
/** * Copyright(c) * All rights reserved. * Author : Mered1th * Date : 2019-02-26-21.04.42 * Description : A1110 */ #include<cstdio> #include<cstring> #include<iostream> #include<cmath> #include<algorithm> #include<string> #include<unordered_set> #include<map> #include<vector> #include<set> #include<queue> using namespace std; struct Node{ int l,r; }node[]; ]={false}; ,n; bool isCBT(int root){ ; queue<int>q; q.push(root); while(!q.empty()){ int top=q.front(); q.pop(); ){ maxd=top; cnt++; q.push(node[top].l); q.push(node[top].r); } else{ if(cnt==n) return true; else return false; } } } int main(){ #ifdef ONLINE_JUDGE #else freopen("1.txt", "r", stdin); #endif cin>>n; string a,b; ;i<n;i++){ cin>>a>>b; ]=='-'){ node[i].l=-; } else{ node[i].l=stoi(a); child[stoi(a)]=true; } ]=='-'){ node[i].r=-; } else{ node[i].r=stoi(b); child[stoi(b)]=true; } } int root; ;i<n;i++){ if(child[i]==false){ root=i; break; } } if(isCBT(root)) printf("YES %d",maxd); else printf("NO %d",root); ; }
这里利用了二叉树静态存储的性质,即父节点和叶子节点之间的关系。
#include<cstdio> #include<cstring> #include<iostream> #include<cmath> #include<algorithm> #include<queue> using namespace std; struct node{ int data; int l,r; }Node[]; ,n; ]={false}; /*void LayerOrder(int root){ queue<int> Q; Q.push(root); while(!Q.empty()){ int now=Q.front(); Q.pop(); if(Node[now].l!=-1){ if(Node[now].r!=-1){ cnt++; Q.push(Node[now].l); } else{ cnt++; } } if(Node[now].r!=-1){ if(Node[now].l!=-1){ cnt++; Q.push(Node[now].r); } else{ cnt++; } } } } */ ,ans; void dfs(int root,int index){ //求出最大下标值,如果下标值等于最大节点数-1则说明是完全二叉树,若大于最大结点数-1则非完全二叉树 if(index>maxn){ maxn=index; ans=root; } ) dfs(Node[root].l,index*); ) dfs(Node[root].r,index*+); } int main(){ #ifdef ONLINE_JUDGE #else freopen("1.txt", "r", stdin); #endif cin>>n; string t1,t2; ;i<n;i++){ cin>>t1>>t2; Node[i].data=i; if(t1=="-"){ Node[i].l=-; } else{ Node[i].l=stoi(t1); hashtable[stoi(t1)]=true; } if(t2=="-"){ Node[i].r=-; } else{ Node[i].r=stoi(t2); hashtable[stoi(t2)]=true; } } int i; ;i<n;i++){ if(hashtable[i]==false) break; } dfs(i,); if(maxn==n){ cout<<"YES "<<ans; } else{ cout<<"NO "<<i; } ; }
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 in ...
- 【PAT甲级】1110 Complete Binary Tree (25分)
题意: 输入一个正整数N(<=20),代表结点个数(0~N-1),接着输入N行每行包括每个结点的左右子结点,'-'表示无该子结点,输出是否是一颗完全二叉树,是的话输出最后一个子结点否则输出根节点 ...
- [二叉树建树&完全二叉树判断] 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 ...
- 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 Advanced 1110 Complete Binary Tree (25) [完全⼆叉树]
题目 Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each ...
- 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 (完全二叉树)
此文章同步发布在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 ...
随机推荐
- DevExpress v18.1新版亮点——WinForms篇(三)
用户界面套包DevExpress v18.1日前终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress WinForms v18.1 的新功能,快来下载试用新版本! ...
- docker容器,镜像常用操作
1.查看正在运行的容器 docker ps 查看所有容器 docker ps -a 2.查看容器日志 docker logs -f showdoc 3.删除所有容器 docker rm $(docke ...
- gradle仓库配置
Android Studio使用Gradle构建app.Gradle的使用非常灵活,其中可以设置使用多种类型的仓库,来获取应用中使用的库文件. 支持的类型有如下几种: 类型 说明 Maven cen ...
- SWIFT中数字格式
SWIFT中格式化数字比较常用的应该就是以下几种格式了. var formatter = NSNumberFormatter() //formatter.numberStyle = NSNumberF ...
- Okhttp之CallServerInterceptor简单分析
在Okhttp源码分析专栏的几篇博客分析了Okhttp几个拦截器的主要功能,还剩下最后一个拦截器CallServerInterceptor没有分析,本篇博客就简单分析下该拦截器的功能. 在Okhttp ...
- GTK安装
上面是linux下GTK+配置所需要的库,关于各个库的功能,查看http://www.gtk.org/overview.php,至于库的下载在http://www.gtk.org/download/l ...
- 第三课 操作系统开发之x86模拟环境搭建
前面我们讲解了主引导程序的加载过程,并且制作了虚拟软盘a.img,最终这个主引导程序也在机器中成功运行了,但是实际开发的时候,并不会如此简单,免不了调试过程,如果还像上一节中直接将软盘放到机器中去加载 ...
- Ubuntu终端及VI 快捷键
Ubuntu终端 快捷键 功能 Tab 自动补全 Ctrl+a 光标移动到开始位置 Ctrl+e 光标移动到最末尾 Ctrl+k 删除此处至末尾的所有内容 Ctrl+u 删除此处至开始的所有内容 Ct ...
- POJ 3617:Best Cow Line(贪心,字典序)
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30684 Accepted: 8185 De ...
- 【java规则引擎】《Drools7.0.0.Final规则引擎教程》第4章 4.2 no-loop
转载至:https://blog.csdn.net/wo541075754/article/details/75201934 no-loop 定义当前的规则是否不允许多次循环执行,默认是 false, ...