#include <stdio.h>
#include <malloc.h>
typedef struct BiTNode
{
int data;
struct BiTNode *lchild,*rchild;
} BiTNode;
int sum=0;
int LeafSum(BiTNode *T)
{
if(T)
{
if(T->lchild==NULL&&T->rchild==NULL)
{
sum+=T->data;
}else{
LeafSum(T->lchild);
LeafSum(T->rchild);
}
}
return sum;
}
void DispBST(BiTNode *bt)
{
if (bt!=NULL)
{
printf("%d",bt->data);
if (bt->lchild!=NULL||bt->rchild!=NULL)
{
printf("(");
DispBST(bt->lchild);
if (bt->rchild!=NULL) printf(",");
DispBST(bt->rchild);
printf(")");
}
}
}
int InsertBST(BiTNode *&p,int k)
{
if (p==NULL)
{
p=(BiTNode *)malloc(sizeof(BiTNode));
p->data=k;
p->lchild=p->rchild=NULL;
return 1;
}
else if (k==p->data)
return 0;
else if (k<p->data)
return InsertBST(p->lchild,k);
else
return InsertBST(p->rchild,k);
}
BiTNode *CreateBST(int A[],int n)
{
BiTNode *bt=NULL;
int i=0;
while (i<n)
{
InsertBST(bt,A[i]);
i++;
}
return bt;
}
int main()
{
BiTNode *bt,*p;
int n=12,x=46;
int a[]= {25,18,46,2,53,39,32,4,74,67,60,11};
bt=CreateBST(a,n);
DispBST(bt);
printf("\n叶节点的值的和为:%d\n",LeafSum(bt));
return 0;
}