# include <stdio.h>
# include <malloc.h>
typedef char ElemType;
typedef struct BiTnode
{
ElemType data;
struct BiTnode * lchild, * rchild;
}BiTNode, * BiTree;
int depth (BiTree &T) //求二叉树深度
{
int depthval = 0;
int depthLeft = 0;
int depthRight = 0;
if (!T)
{
return 0;
}
else
{
depthLeft = depth(T->lchild);
depthRight = depth(T->rchild);
depthval = 1 + (depthLeft > depthRight ? depthLeft : depthRight);
}
return depthval;
}
void countleaf (BiTree &T, int &count) //求叶子节点个数
{
if (T)
{
if (!T->lchild && !T->rchild)
{
++count;
}
else
{
countleaf(T->lchild, count);
countleaf(T->rchild, count);
}
}
return;
}
void create (BiTree &T)
{
char ch;
scanf(" %c", &ch);
if (ch == '#')
{
T = NULL;
}
else
{
T = (BiTree)malloc(sizeof(BiTNode));
T->data = ch;
create(T->lchild);
create(T->rchild);
}
return;
}
void preorder (BiTree &T)
{
if (T)
{
printf("-%c-", T->data);
preorder(T->lchild);
preorder(T->rchild);
}
return;
}
int main(void)
{
BiTree T;
int count = 0;
printf("create a tree such as ABC##DE#G##F###\n");
create(T);
preorder(T);
printf("\n");
countleaf(T, count);
printf("叶子节点个数:%d\n", count);
printf("二叉树深度:%d\n", depth(T));
return 0;
}