33,311
社区成员
发帖
与我相关
我的任务
分享
typedef char ElemType ;
int sum=0;
typedef struct BiTNode
{
ElemType data;
struct BiTNode *Lchild,*Rchild;
}BiTNode,*BiTree;
BiTree Create(BiTree T)
{
char ch;
ch=getchar();
getchar();
if(ch==' ') T=NULL;
else
{
if(!(T=(BiTNode *)malloc(sizeof(BiTNode)))) printf("error.\n");
T->data=ch;
T->Lchild=Create(T);
T->Rchild=Create(T);
}
return T;
}
void Preorder(BiTree T)
{
if(T)
{
printf("%c",T->data);
Preorder(T->Lchild);
Preorder(T->Rchild);
}
}
//无子树的结点设为0,叶子结点设为1,其他是子树的叶子结点之和。
int Sumleaf(BiTree T)
{
if(T)
{
if(T==NULL) sum=0;
else if(T->Lchild==NULL&&T->Rchild==NULL) sum=1;
else sum=Sumleaf(T->Lchild)+Sumleaf(T->Rchild);
return sum;
}
}
int depth(BiTree T)
{
int hl,hr;
if(T)
{
hl=depth(T->Lchild);
hr=depth(T->Rchild);
return 1+(hl>hr?hl:hr);
}
else return 0;
}
void main()
{
BiTree T;
T=Create(T);
Preorder(T);
printf("\n%d\n",Sumleaf(T));
printf("%d\n",depth(T));
}