求二叉樹的高度

2021-08-10 16:24:48 字數 638 閱讀 5454

/*

名稱:求二叉樹的高度。

說明:

(1)、這裡heigh0採用的求二叉樹高度的方法本質上用的是非遞迴後序遍歷的思想。基本的程式流程和非遞迴後序遍歷差不多。在這裡只能用後序遍歷形成的二叉樹,不能用先序或者中序,因為只有後序遍歷的叉樹在棧中,存放的才是一條從根節點到當前節點的路徑,才可以用來計算根節點到當前結點的高度,從而判斷出整個樹的高度。

(2)、heigh1用的是佇列層次遍歷的方法,設定乙個標誌指示每一層的最後乙個結點,每當經過 這個標誌,高度增加1

(3)、heigh2用的是遞迴演算法。

int heigh0(bitree t)

else

//右結點}}

return heigh;

}int heigh1(bitree t)

else

_que.pop(); //當前結點的左右子樹都訪問完,出隊}}

return heigh;

}//遞迴求樹的高度

int heigh2(bitree t)

if(ldep > rdep)

return ldep+

1; else

return rdep+

1;}

求二叉樹高度

函式介面定義 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的高度值。裁判測試程式樣...