版权声明:转载请注明 https://blog.csdn.net/NCC__dapeng/article/details/83817302
这个题。。。有点迷。。。
首先注意题干,人家让你写的是Getheight函数,那个构建树那个函数 是被忽略掉不需要去写的,一开始还在为怎么去构建这个树想了半天。。。。
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
typedef char ElementType;
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
ElementType Data;
BinTree Left;
BinTree Right;
};
BinTree CreatBinTree(); /* 实现细节忽略 */
int GetHeight( BinTree BT );
int main()
{
BinTree BT = CreatBinTree();
printf("%d\n", GetHeight(BT));
return 0;
}
/* 你的代码将被嵌在这里 */
题目解法:简单的递归,遍历每一个分支,遍历次数最多的那个分支加一就是最大的层数
直接给出AC代码:
int GetHeight(BinTree BT)
{
int len1,len2;
if(BT==NULL) return 0;
len1=GetHeight(BT->Left);
len2=GetHeight(BT->Right);
if(len1>len2) return len1+1;
else return len2+1;
}