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行,每行按下 ...
随机推荐
- BCB中换行 需要 \r\n
例如: fprintf(fp,"\n");无换行效果 fprintf(fp,"\r\n");有换行效果
- Codeforces Round #533 (Div. 2) Solution
A. Salem and Sticks 签. #include <bits/stdc++.h> using namespace std; #define N 1010 int n, a[N ...
- Java backup
待补充 ........ 0:常用头文件(待补充) import java.util.Arrays; import java.util.HashSet; import java.util.TreeSe ...
- UnicodeDecodeError: 'ascii' codec can't decode byte 0xbb in position 51: ord
1.问题描述:一个在Django框架下使用Python编写的定时更新项目,在Windows系统下测试无误,在Linux系统下测试,报如下错误: ascii codec can't decode byt ...
- C#反射——简单反射操作类的封装
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Re ...
- centos6.5搭建svn
检查已经安装版本 rpm -qa subversion如果存在旧版本,卸载yum remove subversion 安装svn yum install subversion 验证是否安装成功 sv ...
- PHP Fatal error: Uncaught Error: Call to undefined function pcntl_fork().. 开启php pcntl扩展实现多进程
在使用函数pcntl_fork()时报错 Fatal error: Uncaught Error: Call to undefined function pcntl_fork()....,原因是没有 ...
- Linux点亮一个灯
一 文件及其驱动程序 1.解压linux 压缩包 使用命令: tar xzvf linux-3.0.8-20140925.tgz ( tar xvf ------.tar tar xzvf------ ...
- RabbitMQ 流程以及一些命令
流程: producer&Consumer producer指的是消息生产者,consumer消息的消费者. Queue 消息队列,提供了FIFO的处理机制,具有缓存消息的能力.rabbitm ...
- 双击不能运行可执行的jar文件
1.首先在命令行下运行jar包看文件是否报错(java -jar jar文件名称.jar) 如果程序中有System.out.println()语句,不想让其输出到控制台而保存到文件 ...