本题要求给定二叉树的高度。
函数接口定义:
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 hl, hr, MaxH;
if (BT) {
hl = GetHeight(BT->Left);
hr = GetHeight(BT->Right);
MaxH = (hl > hr)?hl:hr;
return (MaxH + 1);
}
return 0;
}