The number of balanced trees with N node and L leaves An algorithm is requested to calculate all balanced binary trees which can be built with $N$ nodes, having exactly $L$ leaves. A balanced tree is a binary tree in which the difference between the heights of the left and right subtrees is at most 1, and the subtrees are themselves balanced. 

I tried to use dynamic programming to calculate the number for the subtrees created from any subsequence of $1..N$. However, without building each possible subtree and calculating its height and differentiating the height of the subtrees from each other, it can't distinguish the balanced subtrees. Moreover, the sum of their leaves must be exactly $L$. There are many constraints, and I couldn't come up an algorithm, either a working nor an optimum one.

Could you please guide me?

Source: Assignment of Advanced Algorithms, Fall 2018, Tehran University
Suppose $BTC[H][N][L]$ shows the number of balanced trees with the height $H$, with $N$ nodes and $L$ leaves. Using the following formula, a dynamic programming algorithm can be developed for the problem:

The recursive part:
$\begin{align*}
BTC[H][N][L] = \sum_{h=0}^H\sum_{n=0}^N\sum_{l=0}^L \qquad&amp;(BTC[h-1][n][l]\times BTC[h-1][N-n][L-l])\\ 
 +&amp;(BTC[h-1][n][l]\times BTC[h-2][N-n][L-l])\\
 +&amp;(BTC[h-2][n][l]\times BTC[h-1][N-n][L-l])
\end{align*}$

The base cases:
$BTC[h][n][l]=\begin{cases}0 &amp; \text{if $h&gt;\log_2n$ or $l&gt;\frac{n}{2}$ or $n=0$ or $l=0$}\\ 1 &amp; \text{if $h=0$}\end{cases}$