思路如下图所示:
采用递归的思路;黑色的是进入递归时变量的值,红色时返回时值;
核心代码如下:
typedef struct node BTree;
typedef BTree* pBTree;
struct node
{
pBTree Father;
pBTree LeftChild;
pBTree RightChild;
};
int Heigh(pBTree T) //计算树的深度
{
int H=0;
int HL=0;
int HR=0;
if (NULL != T)
{
H++;
HL=Heigh(T->LeftChild); //利用递归计算
HR = Heigh(T->RightChild);
H += (HL >= HR ? HL : HR);
}
return H;
}