// 头文件什么的我就不写了
// 结点的定义
typedef struct node
{
int value;
struct node *lchild;
struct node *rchild;
}Node, *Tree;
// 1.设计算法求二叉树的结点个数
void GetTreeNodeCount( Tree* t, int& count )
{
if( t != NULL )
{
count++;
GetTreeNodeCount( t->lchild, count );
GetTreeNodeCount( t->rchild, count );
}
}
int GetTreeNodeCount( Tree t )
{
int count = 0;
GetTreeNodeCount( t, count );
return count;
}
// 2.设计算法按前序次序打印二叉树中的叶子结点
void PrintLeapNode( Tree t )
{
if( t != NULL )
{
PrintLeapNode( t->lchild );
PrintLeapNode( t->rchild );
if( t->lchild == NULL && t->rchild == NULL )
printf( "%d", t->value );
}
}
// 3.设计算法求二叉树的深度
void GetTreeDeep( Tree t, int i, int& deep )
{
if( t != NULL )
{
GetTreeDeep( t->lchild, i+1, deep );
GetTreeDeep( t->rchild, i+1, deep );
}
else
{
if( i > deep )
deep = i;
}
}
int GetTreeDeep( Tree t )
{
int deep = 0;
GetTreeDeep( t, 0, deep );
return deep;
} |