7-8 List Leaves(25 分)
Given a tree, you are supposed to list all the leaves in the order of top down, and left to right.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤10) 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 test case, print in one line all the leaves' indices in the order of top down, and left to right. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.
Sample Input:
8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6
Sample Output:
4 1 5
用了个结构体,因为结点是确定的,只需要根据数据建立树,找到没有parent结点的作为根,然后层序遍历找叶子。
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h> struct Tree
{
int f,l,r;
}s[];
int n;
void ListLeave(int k)
{
int flag = ;
int a[];
int head = ,tail = ;
a[tail ++] = k;
while(head < tail)
{
if(s[a[head]].l != -)a[tail ++] = s[a[head]].l;
if(s[a[head]].r != -)a[tail ++] = s[a[head]].r;
if(s[a[head]].l == s[a[head]].r)
{
if(flag)printf(" %d",a[head]);
else printf("%d",a[head]);
flag ++;
}
head ++;
}
}
int main()
{
char a,b;
scanf("%d",&n);
for(int i = ;i < n;i ++)
s[i].f = -;
for(int i = ;i < n;i ++)
{
getchar();
scanf("%c %c",&a,&b);
if(a!='-')
{
s[i].l = a - '';
s[a - ''].f = i;
}
else s[i].l = -;
if(b!='-')
{
s[i].r = b - '';
s[b - ''].f = i;
}
else s[i].r = -;
}
for(int i = ;i < n;i ++)
{
if(s[i].f == -)ListLeave(i);
}
}
7-8 List Leaves(25 分)的更多相关文章
- PTA 03-树2 List Leaves (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/666 5-4 List Leaves (25分) Given a tree, you ...
- 03-树2 List Leaves (25 分)
Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. I ...
- 浙大数据结构课后习题 练习三 7-4 List Leaves (25 分)
Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. I ...
- 7-4 List Leaves (25分) JAVA
Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. I ...
- PTA - - 06-图1 列出连通集 (25分)
06-图1 列出连通集 (25分) 给定一个有NN个顶点和EE条边的无向图,请用DFS和BFS分别列出其所有的连通集.假设顶点从0到N-1N−1编号.进行搜索时,假设我们总是从编号最小的顶点出发, ...
- 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)
01-复杂度2 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1,N2, ..., NK }. ...
- PTA 字符串关键字的散列映射(25 分)
7-17 字符串关键字的散列映射(25 分) 给定一系列由大写英文字母组成的字符串关键字和素数P,用移位法定义的散列函数H(Key)将关键字Key中的最后3个字符映射为整数,每个字符占5位:再用除留余 ...
- PTA 旅游规划(25 分)
7-10 旅游规划(25 分) 有了一张自驾旅游路线图,你会知道城市间的高速公路长度.以及该公路要收取的过路费.现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径.如果有若干条 ...
- L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ...
- L2-007 家庭房产 (25 分)
L2-007 家庭房产 (25 分) 给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产套数. 输入格式: 输入第一行给出一个正整数N(≤),随后N行,每行按下 ...
随机推荐
- 《Python网络编程基础》第四章 域名系统
域名系统(DNS) 是一个分布式的数据库,它主要用来把主机名转换成IP地址.DNS以及相关系统之所以存在,主要有以下两个原因: 它们可以使人们比较容易地记住名字,如www.baidu.com. 它 ...
- http://www.kankanews.com/ICkengine/archives/18078.shtml
https://github.com/lealife/WeiXin-Private-API
- esLint参数设置
package.json { "name": "testEsLint", "version": "1.0.0", &qu ...
- 谷歌浏览器&360浏览器安装——有道云笔记插件
谷歌浏览器: 有道云笔记插件:http://hk.chromefor.com/down.php?key=FulQTdJ9In3iXfdVicFW(点击即下载) 在谷歌浏览器里按快捷键:Alt+E 接 ...
- Builder(建造者)
意图: 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. 适用性: 当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式时. 当构造过程必须允许被构造的对象有不同 ...
- postgresql中终止正在执行的SQL语句
在Linux系统中可以使用kill [pid]的方式强制删除进程,但对于修改数据表的语句来说,这样可能导致postgresql进入recovery mode,这样会导致锁表. Postgresql的运 ...
- Android安装包apk文件在某些版本操作系统上安装解析包出错问题的解决办法
当我们将Android升级功能的中的下载新版本apk文件存放在data/data/xxx.apk位置时,在有的些版本的手机中安装可能会出现安装包解析出错的问题,对于该问题的解决方案是提升该文件的权限. ...
- StringUtils 正则校验
public class StringUtils { /** * 如果str为null,返回“”,否则返回str * @param str * @return */ public static Str ...
- UVA-1617 Laptop (贪心)
题目大意:有n条长度为1的线段,n个区间,第i条线段在第i个区间中,问线段之间的最少间隙有几个. 题目分析:先对区间排序,先按右端点排,再按左端点排.有重叠的区间(仅有交点重叠也视为重叠)之间一定可以 ...
- DataReader 绑定DataGridView有两种方式
第一种:借助于BindingSourcesqlDataReader Sdr=通过查询函数得到的sqlDataReader类型的数据;BindingSource Bs=new BindingSource ...