Given n, how many structurally unique BST’s (binary search trees) that store values 1 … n?
Example:
1 | Input: 3 |
Constraints:
1 <= n <= 19
动态规划
令f(i)为把1,2,3,…i放到BST中的所有不同方法数量.
假设要把1,2,3,4…,n这些数放到BST中, 可以从1到n中任意选一个点作为root. 不妨设选择i为root. 那么左子树肯定是1到i-1的这些值, 所以左子树有f(i-1)种可能性. 右子树的值肯定是从i+1到n的这些值, 这些值组成的BST子树的可能数量等于从1到n-i这些值组成的BST子树的可能数量. 所以, 当选择i为root时, 有f(i-1)*f(n-i)种可能. i从1遍历到n, 相加就能得到结果.
1 | class Solution { |