求二叉树中度为0、1、2结点的个数

void leaf(btree* T,int &count)//count 起计数作用 
{
	if(T == NULL) //跳出递归条件 
		return;
	if(T->lchild == NULL&&T->rchild == NULL)//关系运算优先级== 大于逻辑运算&& 
		count++;
	leaf(T->rchild,count);//可以与下式调换位置 
	leaf(T->lchild,count);
}

/*求二叉树中度为1结点个数*/

void degree1(btree* T,int &count)
{
	if(T == NULL)
		return;
	if((T->lchild == NULL &&T->rchild != NULL)||(T->rchild == NULL &&T->lchild != NULL))
		count++;
	degree1(T->lchild,count);
	degree1(T->rchild,count); 
}
/*求二叉树度为2的点*/ 
void degree2(btree *T,int &count)
{
	if(T == NULL)
		return;
	if(T->lchild != NULL&&T->rchild != NULL)
		count++;
	degree2(T->lchild,count);
	degree2(T->rchild,count);
}

你可能感兴趣的:(数据结构)