欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

统计二叉树中度为0,1,2的节点个数

程序员文章站 2022-05-16 18:35:29
...
int NumsDegree_0(BiTree T)
{
	if(T)
	{
		if(T->left == NULL && T->right == NULL)
			return 1;
		else
			return NumsDegree_0(T->left)+NumsDegree_0(T->right);
	}
	else
		return 0;
}

int NumsDegree_1(BiTree T)
{
	if(T)
	{
		if((T->left && T->right==NULL) || (T->right && T->right==NULL))
			return NumsDegree_1(T->left) + NumsDegree_1(T->right) +1;
		else
			return NumsDegree_1(T->left) + NumsDegree_1(T->right);
	}
	else
		return 0;
}

int NumsDegree_2(BiTree T)
{
	if(T)
	{
		if(T->left && T->right)
			return NumsDegree_2(T->left) + NumsDegree_2(T->right) +1;
		else
			return NumsDegree_2(T->left) + NumsDegree_2(T->right);
	}
	else
		return 0;
}