First try to use a binary tree, finally with the structure
struct node{
int k;
int son[101];
};
son[i] Record child nodes
#include <stdio.h>#include<string.h>structnode{intk; intson[101];};intHeightstructNode tree[],intRoot) {//the largest algebra of the family if(tree[root].k==0) return 1; Else{ intmax=0; for(intI=0; i<tree[root].k;i++) if(max<height (tree,tree[root].son[i]) Max=Height (tree,tree[root].son[i]); returnmax+1; }}intSalve (structNode tree[],intLevelintRoot) { if(tree[root].k!=0&&level==1) return 0; Else if(level==1) return 1; Else{ intmax=0; for(intI=0; i<tree[root].k;i++) {max+=salve (tree,level-1, tree[root].son[i]); } returnmax; }};intmain () {structNode tree[101]; Memset (tree,0,101*sizeof(structnode)); intn,m,id,k,count;//0 < N <, the number of nodes in a tree, and M (< N), the number of non-leaf Nodes.scanf"%d%d",&n,&m); for(inti1=0; I1<m;i1++) {//Enter M line, build???? Have a problemscanf"%d%d",&id,&k); TREE[ID].K=k; for(intI2=0; i2<tree[id].k;i2++) {scanf ("%d",&tree[id].son[i2]); } } intMax=height (tree, on); for(intI=1; i<=max;i++){ if(i!=1) printf ("%d", Salve (tree,i, on)); Elseprintf ("%d", Salve (tree,i, on)); } return 0;}
PAT1004 counting Leaves (30) Array Chain-type achievements