If dp[n] stores the number of ways of forming max heap containing n elements, then we have.
dp[n] = nCr(n - 1, n1) * dp[n1] * dp[n2];
i.e.
Select n1 elements out of n - 1 for left subtree.
Elements in left subtree can form max heap in dp[n1] ways.
Elements in right subtree can form max heap in dp[n2] ways.
How to calculate n1 and n2?
I believe that you're missing the loop that encloses the formula you posted. You vary
n1
from1
throughn-1
. If "max heap" requires that you consume alln
nodes, then you have simplyn2 = n-n1
. If you can use fewer, then you need another loop to varyn2
from1
throughn-n1
.Return the sum of all those computed quantities.