6 8 求二叉樹高度 20分

2021-10-07 23:14:45 字數 933 閱讀 4294

本題要求給定二叉樹的高度。

函式介面定義:

int getheight( bintree bt );

其中bintree結構定義如下:

typedef struct tnode *position;

typedef position bintree;

struct tnode;

要求函式返回給定二叉樹bt的高度值。

裁判測試程式樣例:

#include

#include

typedef

char elementtype;

typedef

struct tnode *position;

typedef position bintree;

struct tnode

;bintree creatbintree()

;/* 實現細節忽略 */

intgetheight

( bintree bt )

;int

main()

/* 你的**將被嵌在這裡 */

輸出樣例(對於圖中給出的樹):

思路分析:二叉樹的高度,意即二叉樹的深度,二叉樹的層數,是左右子樹層數的最大值+1(因為單純算左右子樹層數的時候沒有考慮當前根結點的高度,而它實際上也是要參與高度計算的)。遍歷左子樹,遍歷右子樹,找出最大的那個進行+1。這顯然是乙個遞迴的過程。
int

getheight

( bintree bt )

}

6 8 求二叉樹高度 20分

本題要求給定二叉樹的高度。函式介面定義 int getheight bintree bt 其中bintree結構定義如下 typedef struct tnode position typedef position bintree struct tnode 要求函式返回給定二叉樹bt的高度值。裁判測...

6 8 求二叉樹高度 20分

int getheight bintree bt 其中bintree結構定義如下 typedef struct tnode position typedef position bintree struct tnode 要求函式返回給定二叉樹bt的高度值。include include typedef...

6 8 求二叉樹高度 20分

本題要求給定二叉樹的高度。函式介面定義 int getheight bintree bt 其中bintree結構定義如下 typedef struct tnode position typedef position bintree struct tnode 要求函式返回給定二叉樹bt的高度值。裁判測...