【PTA】6-8 求二叉树高度 (20分)

【PTA】6-8 求二叉树高度 (20分)

函数接口定义:

int GetHeight( BinTree BT );

其中BinTree结构定义如下:

typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
    ElementType Data;
    BinTree Left;
    BinTree Right;
};

要求函数返回给定二叉树BT的高度值。

裁判测试程序样例:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 typedef char ElementType;
 5 typedef struct TNode *Position;
 6 typedef Position BinTree;
 7 struct TNode{
 8     ElementType Data;
 9     BinTree Left;
10     BinTree Right;
11 };
12 
13 BinTree CreatBinTree(); /* 实现细节忽略 */
14 int GetHeight( BinTree BT );
15 
16 int main()
17 {
18     BinTree BT = CreatBinTree();
19     printf("%d
", GetHeight(BT));
20     return 0;
21 }
22 /* 你的代码将被嵌在这里 */

输出样例:

4

函数实现细节:

 1 int GetHeight( BinTree BT ){
 2     if(BT==NULL)return 0;
 3     int i,j;
 4     i=GetHeight(BT->Left);
 5     j=GetHeight(BT->Right);
 6     if(i>=j){
 7         return i+1;
 8     }
 9     return j+1;
10 }
原文地址:https://www.cnblogs.com/wyjgr/p/13073551.html