# Count Balanced Binary Trees of Height h

Given a height h, count and return the maximum number of balanced binary trees possible with height h. 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.

**Examples :**

Input : h = 3 Output : 15 Input : h = 4 Output : 315

Following are the balanced binary trees of height 3.

Height of tree, h = 1 + max(left height, right height)

Since the difference between the heights of left and right subtree is not more than one, possible heights of left and right part can be one of the following:

- (h-1), (h-2)
- (h-2), (h-1)
- (h-1), (h-1)

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

Hence we can see that the problem has optimal substructure property.

A **recursive function** to count no of balanced binary trees of height h is:

int countBT(int h) { // One tree is possible with height 0 or 1 if (h == 0 || h == 1) return 1; return countBT(h-1) * (2 *countBT(h-2) + countBT(h-1)); }

The time complexity of this recursive approach will be exponential. The recursion tree for the problem with h = 3 looks like :

As we can see, sub-problems are solved repeatedly. Therefore we store the results as we compute them.

An efficient dynamic programming approach will be as follows :

Below is the implementation of above approach:

## C++

`// C++ program to count number of balanced ` `// binary trees of height h. ` `#include <bits/stdc++.h> ` `#define mod 1000000007 ` `using` `namespace` `std; ` ` ` `long` `long` `int` `countBT(` `int` `h) { ` ` ` ` ` `long` `long` `int` `dp[h + 1]; ` ` ` `//base cases ` ` ` `dp[0] = dp[1] = 1; ` ` ` `for` `(` `int` `i = 2; i <= h; i++) { ` ` ` `dp[i] = (dp[i - 1] * ((2 * dp [i - 2])%mod + dp[i - 1])%mod) % mod; ` ` ` `} ` ` ` `return` `dp[h]; ` `} ` ` ` ` ` `// Driver program ` `int` `main() ` `{ ` ` ` `int` `h = 3; ` ` ` `cout << ` `"No. of balanced binary trees"` ` ` `" of height h is: "` ` ` `<< countBT(h) << endl; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count number of balanced ` `// binary trees of height h. ` `class` `GFG { ` ` ` ` ` `static` `final` `int` `MOD = ` `1000000007` `; ` ` ` ` ` `public` `static` `long` `countBT(` `int` `h) { ` ` ` `long` `[] dp = ` `new` `long` `[h + ` `1` `]; ` ` ` ` ` `// base cases ` ` ` `dp[` `0` `] = ` `1` `; ` ` ` `dp[` `1` `] = ` `1` `; ` ` ` ` ` `for` `(` `int` `i = ` `2` `; i <= h; ++i) ` ` ` `dp[i] = (dp[i - ` `1` `] * ((` `2` `* dp [i - ` `2` `])% MOD + dp[i - ` `1` `]) % MOD) % MOD; ` ` ` ` ` `return` `dp[h]; ` ` ` `} ` ` ` ` ` `// Driver program ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` `int` `h = ` `3` `; ` ` ` `System.out.println(` `"No. of balanced binary trees of height "` `+h+` `" is: "` `+countBT(h)); ` ` ` `} ` `} ` `/* ` `This code is contributed by ` `Brij Raj Kishore ` `*/` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count number of balanced ` `# binary trees of height h. ` ` ` `def` `countBT(h) : ` ` ` `MOD ` `=` `1000000007` ` ` `#initialize list ` ` ` `dp ` `=` `[` `0` `for` `i ` `in` `range` `(h ` `+` `1` `)] ` ` ` ` ` `#base cases ` ` ` `dp[` `0` `] ` `=` `1` ` ` `dp[` `1` `] ` `=` `1` ` ` ` ` `for` `i ` `in` `range` `(` `2` `, h ` `+` `1` `) : ` ` ` `dp[i] ` `=` `(dp[i ` `-` `1` `] ` `*` `((` `2` `*` `dp [i ` `-` `2` `])` `%` `MOD ` `+` `dp[i ` `-` `1` `])` `%` `MOD) ` `%` `MOD ` ` ` ` ` `return` `dp[h] ` ` ` `#Driver program ` `h ` `=` `3` `print` `(` `"No. of balanced binary trees of height "` `+` `str` `(h)` `+` `" is: "` `+` `str` `(countBT(h))) ` ` ` `# This code is contributed by ` `# Brij Raj Kishore ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count number of balanced ` `// binary trees of height h. ` ` ` `using` `System; ` `class` `GFG { ` ` ` ` ` `static` `int` `MOD = 1000000007; ` ` ` ` ` `public` `static` `long` `countBT(` `int` `h) { ` ` ` `long` `[] dp = ` `new` `long` `[h + 1]; ` ` ` ` ` `// base cases ` ` ` `dp[0] = 1; ` ` ` `dp[1] = 1; ` ` ` ` ` `for` `(` `int` `i = 2; i <= h; ++i) ` ` ` `dp[i] = (dp[i - 1] * ((2 * dp [i - 2])% MOD + dp[i - 1]) % MOD) % MOD; ` ` ` ` ` `return` `dp[h]; ` ` ` `} ` ` ` ` ` `// Driver program ` ` ` `static` `void` `Main () { ` ` ` `int` `h = 3; ` ` ` `Console.WriteLine(` `"No. of balanced binary trees of height "` `+h+` `" is: "` `+countBT(h)); ` ` ` `} ` ` ` `// This code is contributed by Ryuga ` `} ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to count ` `// number of balanced ` ` ` `$mod` `=1000000007; ` ` ` `function` `countBT(` `$h` `) ` `{ ` ` ` `global` `$mod` `; ` ` ` ` ` `// base cases ` ` ` `$dp` `[0] = ` `$dp` `[1] = 1; ` ` ` `for` `(` `$i` `= 2; ` `$i` `<= ` `$h` `; ` `$i` `++) ` ` ` `{ ` ` ` `$dp` `[` `$i` `] = (` `$dp` `[` `$i` `- 1] * ` ` ` `((2 * ` `$dp` `[` `$i` `- 2]) % ` ` ` `$mod` `+ ` `$dp` `[` `$i` `- 1]) % ` ` ` `$mod` `) % ` `$mod` `; ` ` ` `} ` ` ` `return` `$dp` `[` `$h` `]; ` `} ` ` ` ` ` `// Driver Code ` `$h` `= 3; ` `echo` `"No. of balanced binary trees"` `, ` ` ` `" of height h is: "` `, ` ` ` `countBT(` `$h` `) ,` `"\n"` `; ` ` ` ` ` `// This code is contributed by aj_36 ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

No of balanced binary trees of height h is: 15

This article is contributed by **Aditi Sharma**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- How to determine if a binary tree is height-balanced?
- Check if a given Binary Tree is height balanced like a Red-Black Tree
- Count the Number of Binary Search Trees present in a Binary Tree
- Minimum count of Full Binary Trees such that the count of leaves is N
- Practice questions on Height balanced/AVL Tree
- Total number of possible Binary Search Trees and Binary Trees with n keys
- Check if the Binary Tree contains a balanced BST of size K
- Height of binary tree considering even level leaves only
- Relationship between number of nodes and height of binary tree
- Iterative Method to find Height of Binary Tree
- Height of a complete binary tree (or Heap) with N nodes
- Create Balanced Binary Tree using its Leaf Nodes without using extra space
- Find Height of Binary Tree represented by Parent array
- Print the nodes of the Binary Tree whose height is a Prime number
- Print middle level of perfect binary tree without finding height
- Find height of a special binary tree whose leaf nodes are connected
- Calculate height of Binary Tree using Inorder and Level Order Traversal
- Enumeration of Binary Trees
- Foldable Binary Trees
- Check if leaf traversal of two Binary Trees is same?