📜  门| Gate IT 2005 |第50章

📅  最后修改于: 2021-07-02 17:07:31             🧑  作者: Mango

在二叉树中,对于每个节点,左子树和右子树中的节点数之差最大为2。如果树的高度为h> 0,则树中的最小节点数为:
(A) 2小时– 1
(B) 2小时– 1 +1
(C) 2小时– 1
(D) 2小时答案: (B)
解释:

Let there be n(h) nodes at height h.

In a perfect tree where every node has exactly 
two children, except leaves, following recurrence holds.

n(h) = 2*n(h-1) + 1

In given case, the numbers of nodes are two less, therefore
n(h) = 2*n(h-1) + 1 - 2
     = 2*n(h-1) - 1

Now if try all options, only option (b) satisfies above recurrence.

Let us see option (B)
n(h) = 2h - 1 + 1

So if we substitute 
n(h-1) = 2h-2 + 1, we should get n(h) = 2h-1 + 1

n(h) =  2*n(h-1) - 1
     =  2*(2h-2 + 1) -1
     =  2h-1 + 1.

这个问题的测验