在学习数据结构树结构时,遇到求解树深度的问题,附上递归法求解树深度。
typedef struct TNode *BinTree;
struct TNode{
int Key;
BinTree Left;
BinTree Right;
};
int heightf(BinTree T)
{
int left = 0, right = 0;
if(T -> Left)
left = heightf(T -> Left);
if(T -> Right)
right = heightf(T -> Right);
if(left > right)
return left + 1;
else
return right + 1;
}