Binary tree height PTA

This problem requires a binary tree of a given height.

Function interface definition:

int GetHeight( BinTree BT );

Wherein BinTree structure is defined as follows:

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

Required function returns the value of a given height of the binary tree BT.

Referee test program Example:

#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;
}
/* 你的代码将被嵌在这里 */

SAMPLE OUTPUT (tree for FIG given):

Here Insert Picture Description

Code:

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);
	}
	else return 0;
}

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/qq_44256227/article/details/89892121