#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;
}
测试