二叉树-叶子节点的个数 C

代码:

#include<stdio.h>
#include<stdlib.h>
typedef struct bitnode
{
	char data;
	struct bitnode *lchild,*rchild;
}bitnode,*bitree;
bitree create(bitree t)
{
	char ch;
	scanf("%c",&ch);
	if(ch=='#')
	t=NULL;
	else
	{
		if(!(t=(bitnode*)malloc(sizeof(bitnode))))
		printf("overflow\n");
		t->data=ch;
		t->lchild=create(t->lchild);
		t->rchild=create(t->rchild);
	}
	return t;
}
int num=0;
void countleaf(bitree t)
{
	if(t)
	{
		if(!(t->lchild)&&!(t->rchild))
		num++;
		countleaf(t->lchild);
		countleaf(t->rchild);	    
	}
}
int main()
{
	bitree t=NULL;
	printf("请输入二叉树先序序列:");	
	t=create(t);
	printf("叶子节点的个数为:\n");
	countleaf(t);
	printf("%d\n",num);
	return 0;
}

图片:

猜你喜欢

转载自blog.csdn.net/Tjhfsghbjknjdy/article/details/85012480