A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.
Input
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.
Output
For each test case, you are supposed to count those family members who have no child for every seniority levelstarting 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
题目大意:
给定一棵树,要求你输出每一层有多少个叶子节点。
分析:
使用深度优先(DFS)或者广度优先(BFS)遍历整棵树。
Java实现
import java.util.Scanner;
import java.util.ArrayList;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n,m;//n是节点个数,M是非叶子节点个数
n = sc.nextInt();
m = sc.nextInt();
ArrayList<ArrayList<Integer>> tree = new ArrayList<>();//用数组结构表示树结构
int[] visited = new int[n];
for(int i=0;i<n; i++){//初始化树结构
tree.add(new ArrayList<Integer>());//加入代表缺省父节点的-1
tree.get(i).add(-1);
visited[i]=0;
}
int tempNo;//代表非叶子节点的代号
int tempNum;//代表非叶子节点的子节点个数
for(int i=0;i<m;i++){
tempNo = sc.nextInt()-1;
tree.get(tempNo).add(-1);
//加入代表缺省层数的-1
tempNum = sc.nextInt();
for(int j=0;j<tempNum;j++){
tree.get(tempNo).add(sc.nextInt()-1);//加入子节点代号
}
}
for(int i=0;i<n;i++){
if(tree.get(i).size()<2){
tree.get(i).add(-1);
}
}
int nextPoint;
for(int j =0;j<tree.size();j++){
if(tree.get(j).size()>2){
for(int i=0;i<tree.get(j).size()-2;i++){
nextPoint = tree.get(j).get(i+2);
tree.get(nextPoint).set(0,j);//初始化各个节点的父节点
}
}
}
ArrayList<Integer> path = new ArrayList<>();//深度搜索的栈
path.add(0);
visited[0]=1;
tree.get(0).set(1,0);
int total[] = new int[n];
for(int i=0;i<n;i++){
total[i]=-1;
}
int have;
int topNode;//栈顶的元素编号
int level=1;
while(path.size()>0){
boolean put = false;
topNode = path.get(path.size()-1);
have=0;
for(int i=0;i<tree.size();i++){
if(tree.get(i).get(0)==topNode && visited[i]==0&&!put){
path.add(i);//将节点入栈
tree.get(i).set(1,path.size()-1);//更新节点的层数
put = true;
visited[i] =1;
if(path.size()>level){
level = path.size();
}
}else
if(tree.get(i).get(0)==topNode && visited[i]==1){
have=1;
}
}
if(!put){
if(have == 1){
path.remove(path.size()-1);
}else
if(have == 0){
path.remove(path.size()-1);
if(total[path.size()]==-1){
total[path.size()] = 1;
}else
total[path.size()]++;
}
}
}
String output="";
for(int i=0;i<level;i++){
if(total[i]==-1)total[i]=0;
if(i!=0){
output += " ";
output += String.valueOf(total[i]);
}else{
output += String.valueOf(total[i]);
}
}
System.out.print(output);
sc.close();
}
}