G
with n+1
elements, all set to 0.G[0]
and G[1]
to 1, as the base cases.j
from 2 to n
(inclusive), compute G[j]
as follows:
a. Initialize G[j]
to 0.
b. For each i
from 1 to j
(as the root), increment G[j]
by G[i-1] * G[j-i]
.G[n]
as the result, which is the number of unique BSTs that can be formed with n
distinct nodes.