二叉树求叶子节点

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define ElemType int





typedef struct BitNode
{
	ElemType weight;
	struct BitNode* lchild, * rchild;
}BitNode, * BiTree;

ElemType GreatTree(BiTree& T)
{
	ElemType data;
	scanf_s("%d", &data);
	if (data == 0)
		T = NULL;
	else
	{
		T = (BiTree)malloc(sizeof(BitNode));
		if (T != NULL)
		{
			T->weight = data;
			printf("输入%d的左子节点:", data);
			GreatTree(T->lchild);
			printf("输入%d的右子节点:", data);
			GreatTree(T->rchild);
		}

	}
	return 1;
}



int Leafdata(BiTree T)
{
	static int wpl = 0;
	if (T->lchild == NULL && T->rchild == NULL)
	{
		printf("%d ", T->weight);
	}
	if (T->lchild != NULL)
	{

		Leafdata(T->lchild);

	}
	if (T->rchild != NULL)
	{

		Leafdata(T->rchild);

	}
	return wpl;
}



int main()
{
	BiTree T;
	printf("输入头节点(0为空节点):");
	GreatTree(T);
	Leafdata(T);
	return 0;

}

测试

 


版权声明:本文为LMZYD原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。