For a complete binary tree with n nodes, how many nodes are leaf nodes?

痞子三分冷 提交于 2019-12-23 12:12:33

问题


One of the answers in our powerpoint says it is n/2 leaves, but I am seeing another answer which says (n+1)/2. I was wondering which one is correct if any, and why?


回答1:


In the simplest case a binary tree with a root node, a left and a right has 3 nodes, two of which are leaf nodes. It's (n+1)/2.




回答2:


If your total number nodes are n , and i are the total number of internal nodes ,i.e., whose degrees are 1. If the tree considered is a binary tree, then this relation holds true.

2i + 3 = n. Root and leaf nodes are not internal nodes. Hence, 2i + 3 = 1 + i + l where l is number of leaf nodes. This gives us, i + 2 = l. and we know that i = (n-3)/2. Hence, l = (n+1)/2. Hope this helps



来源:https://stackoverflow.com/questions/26823382/for-a-complete-binary-tree-with-n-nodes-how-many-nodes-are-leaf-nodes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!