96. Unique Binary Search Trees
# Medium
Dynamic Programming. Build dp[n+1]
to record how many unique BST until ith
. dp[i]
= sum(dp[smaller than root]*dp[greater than root]), root
is changing.
Solution:
Initialize
dp[n+1]
,dp[0]=1
. Think about how to computed[i]
and the value ofdp[1]
should be.Move root until
ith
, sum all of the possible roots, then getdp[i]
.Return
dp[n]
Time complexity = , space complexity =
Last updated