PTA_07_03_树2_list leaves
Given a tree, you are supposed to list all the leaves in the order of top down, and left to right.
给定一棵树,你应该按照从上到下,从左到右的顺序列出所有的叶子。
输入规格
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.
每个输入文件包含一个测试用例。对于每种情况,第一行给出一个正整数N(≤10),这是树中的节点总数——因此节点编号从0到N−1。接下来是N行,每一行对应一个节点,并给出该节点的左右子节点的索引。如果孩子不存在,一个“-”将放在位置。任何一对孩子都被一个空间隔开。
输入样式
8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6
输出规格
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.
对于每个测试用例,按从上到下、从左到右的顺序在一行中打印所有叶子的索引。任何相邻的数字之间必须正好有一个空格,并且在行尾没有额外的空格。
输出样式
4 1 5
算法分析
叶节点的定义:树中没有子结点(度为0)的结点称为是叶节点,叶节点又被称为终端结点
由输入进行分析:
index | 0 | 1 | 2 | 3 | 4 |
---|