树的结构
typedef struct BiTNode
{
char data;
struct BiTNode* lchild, * rchild;
}BiTNode, * BiTree;
代码实现
int GetTreeDepth(BiTree T)
{
int depth1=0;int depth2=0;
if(T==NULL){return 0;}
else
{
depth1=GetTreeDepth(T->lchild);
depth2=GetTreeDepth(T->rchild);
if(depth1>depth2)
{
return depth1+1;
}
else
{
return depth2+1;
}
}
}
代码分析
主要设置左右两个孩子的深度,采用递归形式,从下往上计算深度,到叶子结点的时候,将深度设置为0,然后依次往上加深度,取左右孩子中大的一方进行深度+1