


A binary tree is considered balanced if the difference between the left subtree’s height and the right subtree’s height is less than or equal to 1, for all non-leaf nodes.
Consider N=2, there are three different binary trees.

The first line contains a single integer ‘T’ denoting the number of test cases. The test cases are as follows.
The first line of each test case contains a single integer ‘N’ denoting the height of the balanced binary tree.
For each test case, print the number of balanced binary trees with height ‘N’ modulo 10^9+7.
Print the output of each test case on a new line.
You don’t need to print anything; It has already been taken care of.
1 <= T <= 50
1 <= N <= 10 ^ 4
Where ‘T’ denotes the number of test cases, ‘N’ denotes the height of the balanced binary tree.
Time Limit: 1 sec
For solving the problem, let us first fix the root node at level 1. Since we have fixed the root node we have N-1 levels left, now we have to count the number of ways we can create a balanced binary tree of height (N-1) at the left and the right subtree of the root node; hence we have reduced the problem from finding the number of balanced binary trees with height ‘N’ to finding the number of balanced binary trees with height ‘N-1’. This substructure property paves the way for a recursive solution.
For finding the number of balanced binary trees with height ‘N’, we will fix the root node, and then we will have the following possibilities:
The steps are as follows:
The idea is to use dynamic programming to store the result of a subproblem so that instead of solving the same problem more than once, we can use the stored result.
The steps are as follows:
The idea is to use dynamic programming to store the results of a subproblem and then use the stored results to solve other subproblems.
We will iterate from 1 to ‘N’ and compute and store the result in an array. We will then use the stored results to compute the final answer.
The steps are as follows :
In the previous approach, for calculating the number of balanced binary trees with height “height” we only needed the values of the number of balanced binary trees with height “height-1” and the number of balanced binary trees with height “height-2”. Hence it will be sufficient to store the results of only the previous 2 subproblems. This way we will be able to solve this problem in constant space.
We will iterate from 2 to ‘N’ and compute and store the result of the last 2 iterations in two variables. We will then use the stored results to compute the final answer.
The steps are as follows: