1.求叶子结点数
T的第一个孩子存在T->firstChild里,所以如果T连第一个孩子都没有,那肯定为叶子结点,也就是说T->firstChild==NULL。
所以说,以孩子兄弟表示法存储的森林的叶子结点数 = T->firstChild==NULL 的结点数。
//定义树
typedef struct CSTreeNode{
int data;
struct CSTreeNode *firstChild,*nextSibling;
}CSTreeNode,*CSTree;
//求叶子结点数
//以孩子兄弟表示法存储的森林的叶子结点数 = firstChild为空的 结点数
void leaves(CSTree T,int &count){
if(T){
if(T->firstChild == NULL)
count++;
leaves(T->firstChild,count);
leaves(T->nextSibling,count);
}
}
int leaves(CSTree T){
if(T == NULL)
return 0;
if(T->firstChild == NULL)
return leaves(T->nextSibling) + 1;
else
return leaves(T->firstChild) + leaves(T->nextSibli