我知道樹中的節點總數。用這個如何找到樹的高度?如何找到二叉樹的高度,如果我有節點總數
0
A
回答
0
如果你有一棵平衡樹,那麼高度(層數)就是上限(lg(n + 1)),其中天花板總是舍入到下一個最高整數,而lg是以2爲底的對數。如果你有一定類型的自平衡樹(它們並不總是完全平衡的,但高度平衡),那麼也可以給出樹的高度的信息界限。如果你只是假設一個任意的二叉樹(比如說,每個內部節點有兩個孩子),那麼高度可能會高達(n-1)/ 2,例如如果你有一棵看起來像梳子的樹。
+0
@ jwpat7:log2(3 + 1)= 2,所以ceil(log2(3 + 1))= 2。 log2(4 + 1)= 2.32 ...所以ceil(log2(4 + 1))= 3。這似乎與你的身高值有關。 – rici
0
Create a queue.
Push root into the queue.
height = 0
Loop
nodeCount = size of queue
// If number of nodes at this level is 0, return height
if nodeCount is 0
return Height;
else
increase Height
// Remove nodes of this level and add nodes of
// next level
while (nodeCount > 0)
pop node from front
push its children to queue
decrease nodeCount
// At this point, queue has nodes of next level
欲瞭解更多信息:Click this link
+0
什麼?你不能在downvoting之前編輯或發表評論嗎? – MoraRockey
相關問題
- 1. L葉節點的二叉樹高度
- 2. 如何在二叉樹中找到節點的父節點?
- 3. 查找二叉樹高度
- 4. 以遞歸方式查找二叉樹中節點高度的總和
- 5. 二叉搜索樹的總高度
- 6. 如何計算二叉樹中的節點總數
- 7. 查找二叉查找樹的高度
- 8. 如何找到非二叉樹的高度?
- 9. 二叉樹高度
- 10. 二叉樹高度函數
- 11. 如何找到非二叉樹中的第n個節點?
- 12. 二叉樹節點計數
- 13. 返回二叉查找樹的高度
- 14. 查找非二叉樹的高度
- 15. 無法找出二叉樹的高度
- 16. 找出二叉樹的高度
- 17. 如何找到節點值比二叉搜索樹指定值
- 18. 二叉樹中節點的深度
- 19. 查找二叉樹的最深節點
- 20. 查找二叉樹中的節點
- 21. 在二叉搜索樹中找到節點的父節點
- 22. 二叉樹的高度
- 23. 如何查找並返回二叉樹的最底部(最深節點)節點?二叉搜索樹?
- 24. Prolog。二叉樹的節點
- 25. 用二叉樹混合查找節點
- 26. 二叉樹中特定節點的高度
- 27. 如何高效地從特定節點產生二叉樹?
- 28. 具有n節點和n-3高度的二叉樹的數量
- 29. 非二叉樹高度
- 30. Java二叉樹高度
你必須假定它是平衡的,也不能這樣做(除非你想些沒用的邊界)。 – Teepeemm