PAT 1004 Counting Leaves (30分) BFS找每一层非叶子节点数

Scroll Down

题目

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: 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 01 is the root and 02 is its only child. Hence on the root 01 level, there is 0 leaf node; and on the next level, there is 1 leaf node. Then we should output 0 1 in a line.

Sample Input:
2 1
01 1 02
Sample Output:
0 1

题目大意

翻译过来就是,有一棵树,给出了节点数N(根节点编号为01),非叶子节点M,给出了每个非叶子节点的孩子个数 K,每个孩子的编号

输出 每一层有几个叶子节点

思路分析

  • 不用构建树的结构体,首先我们不用区分孩子的顺序,知道它有几个孩子,编号是几就可以了;其次,每个非叶子节点所拥有的孩子数目是不一样的,也无法构建结构体。
  • 所以,我们用一个vector数组,每个vector保存一个非叶子节点的所有孩子编号。
  • 判断是否是叶子节点很简单,只需要判断它对应的vector的size是否为0就可以。
  • 由于我们不能直接确定树的结构,而最后输出每一层的非叶子节点个数,所以需要一个变量maxLevel保存这个数有多高,也就是最深一层是哪一层。
  • 然后我们要求得每一层的叶子节点有几个,所以还得用一个数组保存每一层的叶子节点数。
  • 接下来,从根节点开始,进行递归就好了
	// 根节点编号是1,层级是0 
	helper(1, 0);
	// index 节点编号; level 节点在哪一层
	void helper(int index, int level) {
		// 他没有孩子,它是叶子节点,
		if (nodes[index].size() == 0) {
			// 他所在这一层叶子节点数+1 
			levelLeaves[level]++;
			// 更新 这棵树的最深的有效层
			maxLevel = max(maxLevel, level);
			return;
		}
		// 他的所有孩子都处于他的下一层 
		for (int i = 0; i < nodes[index].size(); ++i) 
			helper(nodes[index][i], level + 1);
	}
} 

完整代码

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

// 保存每个节点的全部孩子,自己的编号作为下标,值是一个vector 
vector<int> nodes[100];
// 保存每一层有几个叶子节点 
int levelLeaves[100];
// 题目最多有100个节点,保存最深的有效层是哪一层

int maxLevel = -1; 

//
void helper(int index, int level) {
	// 他没有孩子,他所在这一层叶子节点数+1 
	if (nodes[index].size() == 0) {
		levelLeaves[level]++;
		// 更新 最深的有效层
		maxLevel = max(maxLevel, level);
		return;
	}
	// 他的所有孩子都处于他的下一层 
	for (int i = 0; i < nodes[index].size(); ++i) 
		helper(nodes[index][i], level + 1);
} 

int main() {
	
	// n个节点,m个非叶子节点 
	int n, m;
	
	int index, kids, child;
	
	cin >> n >> m;
	
	// 每一行是一个非叶子节点的情况
	// 编号 几个孩子 孩子1编号 孩子2编号 ... 
	for (int i = 0; i < m; ++i) {
		cin >> index >> kids;
		for (int j = 0; j < kids; ++j) {
			cin >> child;
			nodes[index].push_back(child);
		}
	} 
	
	// 根节点编号是1,层级是0 
	helper(1, 0);
	
	// 这样写会导致最终多出来一个空格,不满足输出要求	
//	for (int i = 0; i < maxLevel; ++i)
//		cout << levelLeaves[i] << " ";
	// 输出每一层的叶子节点个数
	cout << levelLeaves[0];
	// maxLevel是最深层 
	for (int i = 1; i <= maxLevel; ++i)
		cout << " " << levelLeaves[i]; 

    return 0;
}