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.
1 3 3 2 1 / / / 3 2 1 1 3 2 / / 2 1 2 3
解题思路:
dynamic programming. 最重要的就是找规律!!!
这类找combination/permutation的题都需要找找规律。
n = 0
n = 1
1
n = 2
1 2
/
2 1
n = 3
1 3 3 2 1
/ / /
3 2 1 1 3 2
/ /
2 1 2 3
定义f(n)为unique BST的数量,以n = 3为例:
构造的BST的根节点可以取{1, 2, 3}中的任一数字。
如以1为节点,则left subtree只能有0个节点,而right subtree有2, 3两个节点。所以left/right subtree一共的combination数量为:f(0) * f(2) = 2
以2为节点,则left subtree只能为1,right subtree只能为2:f(1) * f(1) = 1
以3为节点,则left subtree有1, 2两个节点,right subtree有0个节点:f(2)*f(0) = 2
总结规律:
f(0) = 1
f(n) = f(0)*f(n-1) + f(1)*f(n-2) + ... + f(n-2)*f(1) + f(n-1)*f(0)
Let count[i] be the number of unique binary search trees for i. The number of trees are determined by the number of subtrees which have different root node. For example,
i=0, count[0]=1 //empty tree
i=1, count[1]=1 //one tree
i=2, count[2]=count[0]*count[1] // 0 is root
+ count[1]*count[0] // 1 is root
i=3, count[3]=count[0]*count[2] // 1 is root
+ count[1]*count[1] // 2 is root
+ count[2]*count[0] // 3 is root
i=4, count[4]=count[0]*count[3] // 1 is root
+ count[1]*count[2] // 2 is root
+ count[2]*count[1] // 3 is root
+ count[3]*count[0] // 4 is root
..
..
..
Recurrence formula: i=n, count[n] = sum(count[0..k]*count[k+1...n]) 0 <= k < n-1
base case: count[0] = 1 count[1] = 1
Java code:
public class Solution { public int numTrees(int n) { int[] count = new int[n+1]; count[0] = 1; count[1] = 1; for(int i = 2; i<= n; i++){ for(int j = 0; j < i; j++){ count[i] += count[j] * count[i-1-j]; } } return count[n]; } }
Reference:
1. http://www.programcreek.com/2014/05/leetcode-unique-binary-search-trees-java/
2. http://bangbingsyb.blogspot.com/2014/11/leetcode-unique-binary-search-trees-i-ii.html