BBT counter | Practice | GeeksforGeeks

Given a height h, count the maximum number of balanced binary trees possible with height h. Print the result modulo 109 + 7.
Note : A balanced binary tree is one in which for every node, the difference between heights of left and right subtree is not more than 1.

Example 1:
Input: h = 2
Output: 3 
Explanation: The maximum number of balanced 
binary trees possible with height 2 is 3. 


Example 2:
Input: h = 3
Output: 15
Explanation: The maximum number of balanced
binary trees possible with height 3 is 15. 


Your Task:  
You dont need to read input or print anything. Complete the function countBT() which takes h as input parameter and returns the maximum number of balanced binary trees possible with height h. 

Expected Time Complexity: O(h)
Expected Auxiliary Space: O(h)

Constraints:
1<= n <=103
0
jainmuskan5652 weeks ago
long long int countBT(int h) {        int mod= 1e9+7;       vector<long long int> bbt(h+1);       bbt[0]=1;       bbt[1]=1;       for(int i=2;i<=h;i++){           // difference can be 1 to height of left subtree and right subtree           // can be i-1, i-1/ i-1, i-2/ i-2,i-1           // multiplying all the counts from both side           // adding all the cases           bbt[i]= ((bbt[i-1]*bbt[i-1])%mod + (2*(bbt[i-1]*bbt[i-2]))%mod)%mod;       }       return bbt[h];   }
+9
hritesh10013 months ago
I don't think it is an EASY problem.It is at least a MEDIUM one.
+1
shivaraj54 months ago
100% working
const int mod=1e9+7;
long long int countBT(int h) {        // code here       long long dp[h+1];       dp[0]=1;       dp[1]=1;       for(long long i=2;i<=h;i++){           dp[i]=(dp[i-1]%mod*dp[i-2]%mod+dp[i-2]%mod*dp[i-1]%mod+                   dp[i-1]%mod*dp[i-1]%mod)%mod;       }       return dp[h];          }
0
shivaraj5
This comment was deleted.
-1
sunnyjha15120024 months ago
    // code here
   int dp[h+1];
    dp[0]=1;
    dp[1]=1;
    int mod = (int) (pow(10,9)+ 7);
    for(int i=2 ;i<=h; i++)
    {
        dp[i]=(int)( ( (long)(dp[i-1])*dp[i-1] )%mod + (2*(long)(dp[i-1])*dp[i-2])%mod  ) % mod;
    }
return dp[h];  
+1
shubhamprashar7 months ago
TYPICAL GFG TLE for Top down dp Python
0
Sayantan Sinharay8 months ago
Sayantan Sinharay
why recursion is not working??
0
thin_k_ing10 months ago
thin_k_ing
A BBT of height h can have subtrees as (h - 1, h - 2), (h - 1, h - 1) or, (h - 2, h - 1).ll countBT(int h) {         int p = 1e9 + 7;        vector<ll> bbt(h + 1);        bbt[0] = 1, bbt[1] = 1;        for(int i = 2; i <= h; i++)            bbt[i] = (bbt[i - 1] * (bbt[i - 1] + 2 * bbt[i - 2])) % p;        return bbt[h];    }
0
suraj kumar11 months ago
suraj kumar
execution time 0.01long long int countBT(int h) {         // code here        const long long int mod = 1e9+7;        long long int dp[h+1];        memset(dp,0,sizeof(dp));        dp[0]=1;        dp[1] = 1;        for(int i=2;i<=h;i++)        {            dp[i] = (dp[i]%mod + (dp[i-1]*dp[i-2])%mod)%mod;            dp[i] = (dp[i]%mod + (dp[i-2]*dp[i-1])%mod)%mod;            dp[i] = (dp[i]%mod + (dp[i-1]*dp[i-1])%mod)%mod;        }        return (dp[h]%mod)%mod;    }
0
sk bhau
This comment was deleted.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.