本题要求给定二叉树的高度。
函数接口定义:
int GetHeight( BinTree BT );
其中BinTree
结构定义如下:
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
ElementType Data;
BinTree Left;
BinTree Right;
};
要求函数返回给定二叉树BT的高度值。
实现:
int GetHeight( BinTree BT )
{
int l, r;
if (BT == NULL)
return 0;
l = GetHeight(BT->Left);
r = GetHeight(BT->Right);
return (l > r ? l : r) + 1;
}