The number of balanced trees with N node and L leaves
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}$