求二叉樹的高度

2021-08-04 09:31:35 字數 718 閱讀 3262

這裡計算二叉樹的高度採用三種不同的演算法。

演算法一:後序遍歷二叉樹,結點最大棧長即為二叉樹的高度;

演算法二:層次遍歷二叉樹,最大層次即為二叉樹的高度;

演算法三:採用遞迴演算法,求二叉樹的高度。

//法1:後序遍歷,結點最大棧長即為樹的高度

//法2:層次遍歷,層次即為高度

//法3:遞迴演算法求樹高

#include

#include

#include

using

namespace

std;

typedef

struct bitnodebitnode,*bitree;

void createtree(bitree &t)

}

//法1:後序遍歷,結點最大棧長即為樹的高度 

int bt_high(bitree t)

else

} }

return

max;

}

//法2:層次遍歷,層次即為高度 

int bt_level_depth(bitree t)

} return level;

}

int height (bitree t) 

求二叉樹高度

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

求二叉樹高度

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

求二叉樹高度

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