GATE CSE
First time here? Checkout the FAQ!
x
0 votes
31 views

I have a doubt in this question :

http://gateoverflow.in/3811/gate2005-it_50

I am posting this, as there is a very low probability my comment will be replied.

I wanted to progress in the solution by forming the recurrence . This was my logic :

T(h) : No. of nodes at height h

T(l,h-1) : No. of nodes in the left subtree having height h-1

T(r,h-1) : No. of nodes in the right subtree having height h-1

T(h) = T(l,h-1)+T(r,h-1)+1

Min. no. of nodes occur when diff between T(l,h-1) and T(r,h-1) is 2.

Recurrence :

T(h) = T(r,h-1)+2+T(r,h-1)+1

T(h)=2T(h-1) + 3

This is not correct, as T(1) should be 2. It gives 5; T(0)=1

Can anynody help me with correct recurrence?

asked in Algorithms by Loyal (3.7k points)   | 31 views

1 Answer

+1 vote
Best answer
- You almost got that..

Min. no. of nodes occur when diff between T(l,h-1) and T(r,h-1) is 2.

- Yes, correct till this.

Recurrence :

T(h) = T(r,h-1)+2+T(r,h-1)+1

- Here, you are replacing T(l,h-1) with T(r,h-1) + 2. That means you are adding 2 more nodes to left subtree.

Why add 2 more nodes? We need min number of nodes, so just subtract 2 nodes.. i.e

Recurrence :

T(h) = T(r,h-1)-2+T(r,h-1)+1

T(h)=2T(h-1) - 1;

T(1)=2 This should be considered as base condition as h>0
answered by Loyal (2.9k points)  
selected by
Thanks!! Your explanation is correct
Top Users Feb 2017
  1. Arjun

    5224 Points

  2. Bikram

    4230 Points

  3. Habibkhan

    3748 Points

  4. Aboveallplayer

    2986 Points

  5. Debashish Deka

    2356 Points

  6. sriv_shubham

    2298 Points

  7. Smriti012

    2142 Points

  8. Arnabi

    2008 Points

  9. sh!va

    1654 Points

  10. mcjoshi

    1628 Points

Monthly Topper: Rs. 500 gift card

20,832 questions
25,989 answers
59,623 comments
22,046 users