A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.
Input Specification:
Each input file contains one test case. Each case starts with a line containing 0<N<100, the number of nodes in a tree, and M (<N), the number of non-leaf nodes. Then M lines follow, each in the format:
1 | ID K ID[1] ID[2] ... ID[K] |
where ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID’s of its children. For the sake of simplicity, let us fix the root ID to be 01.
The input ends with N being 0. That case must NOT be processed.
Output Specification:
For each test case, you are supposed to count those family members who have no child for every seniority level starting from the root. The numbers must be printed in a line, separated by a space, and there must be no extra space at the end of each line.
The sample case represents a tree with only 2 nodes, where is the root and is its only child. Hence on the root level, there is leaf node; and on the next level, there is leaf node. Then we should output in a line.
Sample Input:
2 1
01 1 02
Sample Output:
0 1
题目翻译:
家谱通常是用系谱树来表示,你的工作是计算出那些没有孩子的家庭成员。
输入规范:
每个输入文件包含一个测试实例,每个实例开始于一个包含N个结点(系谱树的成员结点数),且0<N<100的一行(也就是每个数字代表每个子树的成员结点数),然后是M(系谱树的非叶子结点数),M<N,M的格式如下:
1 | ID K ID[1] ID[2] ... ID[K] |
其中id是表示给定非叶节点的两位数字,K是孩子结点个数,后面跟的是表示孩子结点的ID序列,为了简单起见,我们将根ID设置为01.
输入N以0结尾时,这种情况不被处理
输出规范:
对于每个测试用例,你应该从根结点开始计算每层那些没有孩子的家庭成员,这些数需要用一行打印出来,使用空格分隔,且没有额外的空格在每一行。
在示例用例中是一个只有两个结点的树,01是根,02是唯一的孩子,因此根在01层,有0个叶子结点,在下一层中,有一个叶子接地只能,所以我们应该在一行输出0 1 。
思路:
明显的树的遍历(层次遍历),大致题意是输出二叉树的每一层的叶结点个数.
先用二维数组存储一个二维数组的根节点和它们的子结点,然后使用dfs来遍历每一层,并记录每一层叶结点数量