96. Unique Binary Search Trees
Given n, how many structurally unique BST’s (binary search trees) that store values 1…n?
For example,
Given n = 3, there are a total of 5 unique BST’s.
|
|
求方案的个数,一般是用动归做
- State: dp[i] 指有i个节点的BST有多少种组成方案
- Function: dp[i] = dp[0] dp[i - 1] + dp[1] dp[i - 2] + … + dp[i - 1] * dp[0]
- Init: dp[0] = 1
- Answer: dp[n]
|
|