陣列表示的二叉樹

2021-08-22 08:56:57 字數 703 閱讀 5861

面試某廠的時候面試官問了二叉樹最長路徑的演算法題,給定乙個陣列找出最長路徑的值,第一反應就是動態規劃,結果手寫**的時候懵逼想不起怎麼通過子節點推算父節點的下標,特此記錄警世自己。

fun main(args: array) 

class tree 對應的父節點是 $")

}}

輸出:

index 1 值是6 對應的父節點是 9

index 2 值是4 對應的父節點是 9

index 3 值是8 對應的父節點是 6

index 4 值是3 對應的父節點是 6

index 5 值是7 對應的父節點是 4

index 6 值是2 對應的父節點是 4

index 7 值是10 對應的父節點是 8

index 8 值是25 對應的父節點是 8

index 9 值是7 對應的父節點是 3

面試題的**解法如:

fun maxlen(): int 

}println("最長路徑:")

tmp.foreach

return tmp[tmp.lastindex]

}

輸出:

最長路徑:

0,6,6,14,14,14,14,24,39,39,

二叉樹用陣列表示

複雜的程式設計,思考永遠是最重要的 include stdio.h include stdlib.h include math.h int b tree 20 int count 0 因為count不能由開始輸入的資料個數直接看出來 故我直接用全域性 變數方便隨時使用 這段程式我借鑑了鍊錶建立二叉樹...

二叉樹的表示

二叉鏈表示法 struct bittree typedef struct bittree bitnode typedef struct bittree bitreee int main void include include include 第二種表示方法 三叉鍊錶 三叉鍊錶 typedef st...

非二叉樹轉二叉樹的表示法

樹的儲存結構,詳細 分析在 秦玉平 馬靖善所編的資料結構 第三版 p137 142 include define max 100 typedef char elemtype 雙親表示法 typedef structptnode typedef structptree 孩子雙親表示法,若需要孩子鍊錶表...