函数题:计算二叉树的高度
计算二叉树的高度并输出。
### 函数接口定义:
c++
int GetHeight( BinTree BT )
其中,BinTree 的结构定义为:
c++
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
ElementType Data;
BinTree Left;
BinTree Right;
};
### 裁判测试程序样例:
c++
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
typedef struct TNode * BinTree; /* 二叉树类型 */
typedef char ElementType;
struct TNode{ /* 树结点定义 */
ElementType Data; /* 结点数据 */
BinTree Left; /* 指向左子树 */
BinTree Right; /* 指向右子树 */
};
int GetHeight( BinTree BT )
//按先序次序输入二叉树中结点的值(一个字符),@表示空树,构造二叉链表表示二叉树T
BinTree CreatBinTree()
{
ElementType ch;
BinTree T;
scanf("%c",&ch); /*按先序次序输入树的结点,空树输入@*/
if(ch == '@')
T = NULL;
else {
T = (BinTree)malloc(sizeof(struct TNode));
T->Data = ch;
T->Left = CreatBinTree();
T->Right = CreatBinTree();
}
return T;
}
int main()
{
BinTree BT;
int height;
BT = CreatBinTree();
if(BT == NULL){
printf("\n空树!\n");
}else{
height = GetHeight(BT);
printf("该二叉树的高度为:%d",height);
}
return 0;
}
/* 请在这里填写答案 */

### 输入样例:
in
ABD@@EG@@@C@F@@
### 输出样例:
out
该二叉树的高度为:4
答案:若无答案欢迎评论
### 函数接口定义:
c++
int GetHeight( BinTree BT )
其中,BinTree 的结构定义为:
c++
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
ElementType Data;
BinTree Left;
BinTree Right;
};
### 裁判测试程序样例:
c++
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
typedef struct TNode * BinTree; /* 二叉树类型 */
typedef char ElementType;
struct TNode{ /* 树结点定义 */
ElementType Data; /* 结点数据 */
BinTree Left; /* 指向左子树 */
BinTree Right; /* 指向右子树 */
};
int GetHeight( BinTree BT )
//按先序次序输入二叉树中结点的值(一个字符),@表示空树,构造二叉链表表示二叉树T
BinTree CreatBinTree()
{
ElementType ch;
BinTree T;
scanf("%c",&ch); /*按先序次序输入树的结点,空树输入@*/
if(ch == '@')
T = NULL;
else {
T = (BinTree)malloc(sizeof(struct TNode));
T->Data = ch;
T->Left = CreatBinTree();
T->Right = CreatBinTree();
}
return T;
}
int main()
{
BinTree BT;
int height;
BT = CreatBinTree();
if(BT == NULL){
printf("\n空树!\n");
}else{
height = GetHeight(BT);
printf("该二叉树的高度为:%d",height);
}
return 0;
}
/* 请在这里填写答案 */

### 输入样例:
in
ABD@@EG@@@C@F@@
### 输出样例:
out
该二叉树的高度为:4
答案:若无答案欢迎评论