96. Unique Binary Search Trees

Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n?

Example:

Input: 3
Output: 5
Explanation:
Given n = 3, there are a total of 5 unique BST's:

   1         3     3      2      1
    \       /     /      / \      \
     3     2     1      1   3      2
    /     /       \                 \
   2     1         2                 3

核心思路: 子问题为 分别以 1...n 为根, 左右子树的个数和 dp 的关系

solution from this post

we need count how many possible trees are there constructed from {2,3,4,5}, apparently it's the same number as {1,2,3,4}

/**
 * @param {number} n
 * @return {number}
 */
var numTrees = function(n) {
  if (n===0) return 0
  var dp=[1,1,2]
  for(var i=3;i<=n;i++) {
    for (var j=1;j<=i;j++) {
      dp[i]+=dp[j-1]*dp[i-j]
    }
  }
  return dp[n]
};

results matching ""

    No results matching ""