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 分)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 浙大数据结构课后习题 练习三 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 ...

  4. 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 ...

  5. PTA - - 06-图1 列出连通集 (25分)

    06-图1 列出连通集   (25分) 给定一个有NN个顶点和EE条边的无向图,请用DFS和BFS分别列出其所有的连通集.假设顶点从0到N-1N−1编号.进行搜索时,假设我们总是从编号最小的顶点出发, ...

  6. 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)

    01-复杂度2 Maximum Subsequence Sum   (25分) Given a sequence of K integers { N​1​​,N​2​​, ..., N​K​​ }. ...

  7. PTA 字符串关键字的散列映射(25 分)

    7-17 字符串关键字的散列映射(25 分) 给定一系列由大写英文字母组成的字符串关键字和素数P,用移位法定义的散列函数H(Key)将关键字Key中的最后3个字符映射为整数,每个字符占5位:再用除留余 ...

  8. PTA 旅游规划(25 分)

    7-10 旅游规划(25 分) 有了一张自驾旅游路线图,你会知道城市间的高速公路长度.以及该公路要收取的过路费.现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径.如果有若干条 ...

  9. L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ...

  10. L2-007 家庭房产 (25 分)

    L2-007 家庭房产 (25 分)   给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产套数. 输入格式: 输入第一行给出一个正整数N(≤),随后N行,每行按下 ...

随机推荐

  1. C语言头文件#include<stdlib.h>的作用

      stdlib 头文件即standard library标准库头文件 stdlib 头文件里包含了C.C++语言的最常用的系统函数 该文件包含了的C语言标准库函数的定义 stdlib.h里面定义了五 ...

  2. Java基础知识陷阱(九)

    本文发表于本人博客. 今天我来说说关于JAVA多线程知识,有错误请指出.大家都知道JAVA在服务端上处理也有很大优势,很多公司也有在服务器跑JAVA进程,这说明JAVA在处理这个多线程以及并发下也有一 ...

  3. java8中接口中的default方法

    在java8以后,接口中可以添加使用default或者static修饰的方法,在这里我们只讨论default方法,default修饰方法只能在接口中使用,在接口种被default标记的方法为普通方法, ...

  4. dns之缓存。

    1.浏览器缓存.这里以chrome为例.在chrome上输入:chrome://net-internals/#dns 可以查看chrome浏览器的dns缓存信息. 这样. 2.windows系统缓存. ...

  5. 对Java平台的理解

    1)  Java是一种面向对象的语言(封装,继承,多态),最显著的特性有两个方面: ----书写一次,到处运行(Write once,run anywhere) 能够非常容易的获得跨平台的能力 --- ...

  6. pyDay11

    内容来自廖雪峰的官方网站. 1.杨辉三角generator: def triangles(): L = [1] while True: yield L L.append(0) L = [L[i-1] ...

  7. 20145211《网络对抗》注入Shellcode并执行&&Return-to-libc攻击

    Shellcode注入 基础知识 Shellcode实际是一段代码,但却作为数据发送给受攻击服务器,将代码存储到对方的堆栈中,并将堆栈的返回地址利用缓冲区溢出,覆盖成为指向 shellcode的地址. ...

  8. 【转载】make: Nothing to be done for `all'. 解决方法

    转自:make: Nothing to be done for `all'.解决方法 make: Nothing to be done for `all' 解决方法 1.这句提示是说明你已经编译好了, ...

  9. Android -- 怎么发出和接收广播, Broadcast, 电话拨号拦截,短信拦截

    1. 发送广播 使用以下三个API可以发送广播 public void click(View view){ Intent intent = new Intent(); intent.setAction ...

  10. PostMessage与SendMessage的区别

    PostMessage只负责将消息放到消息队列中,不确定何时及是否处理 SendMessage要等到受到消息处理的返回码(DWord类型)后才继续 PostMessage执行后马上返回 SendMes ...