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. 5.4 Components -- Wrapping Content in A Component(在组件中包裹内容)

    1.有时候,你可能希望定义一个模板,它包裹其他模板提供的内容. 例如,假设我们创建一个blog-post模板,我们可以使用它来展现一个blog post: app/components/blog-po ...

  2. vuex的一个坑

    1  error in callback for watcher "function (){ return this._data.$$state }" 用深拷贝解决 2 接口依赖: ...

  3. Educational Codeforces Round 59 Solution

    A. Digits Sequence Dividing 签. #include <bits/stdc++.h> using namespace std; #define N 1010 ch ...

  4. MapReduce 过程详解 (用WordCount作为例子)

    本文转自 http://www.cnblogs.com/npumenglei/ .... 先创建两个文本文件, 作为我们例子的输入: File 1 内容: My name is Tony My com ...

  5. Web安全之BurpSuite抓取HTTPS请求

    出现了问题,第一步要干什么呢? 当然是要去官方网站去找FAQ和help,先来练习一下英语 https://portswigger.net/burp/help/proxy_options_install ...

  6. HBase参数优化

    zookeeper.session.timeout默认值:3分钟(180000ms)说明:RegionServer与Zookeeper间的连接超时时间.当超时时间到后,ReigonServer会被Zo ...

  7. HDU 1438 钥匙计数之一(状压DP)题解

    思路: 每个槽有4种深度,一共有2^4种状态.然后开4维来保存每一次的状态:dp[ 第几个槽 ][ 当前状态 ][ 末尾深度 ][ 是否符合要求 ]. 代码: #include<cstdio&g ...

  8. reason: image not found的解决方案

    在制作framework时遇到真机运行时导致的reason: image not found允许崩溃的问题,下面是我的解决方案: 首先我们分析一下出现这种情况的原因,原因就是framework找不到镜 ...

  9. CCNA学习指南 -开放最短路径优先OSPF(多区域部分)

    在之前的介绍中,可以看到单区域OSPF对于古老的RIP的优点: 路由选择更新流量减小 使用与大型网络和链路速度不一样的网络 OSPF能够在LSDB中呈现网络拓扑结构,这使得它汇聚的速度远快于RIP. ...

  10. UVa 10328 Coin Toss(Java大数+递推)

    https://vjudge.net/problem/UVA-10328 题意: 有H和T两个字符,现在要排成n位的字符串,求至少有k个字符连续的方案数. 思路:这道题目和ZOJ3747是差不多的,具 ...