📜  门|门 IT 2005 |第 50 题

📅  最后修改于: 2021-09-24 05:54:31             🧑  作者: Mango

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

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.

这个问题的测验