keep moving

# 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.

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


public int numTrees(int n) {
return getNum(1, n);
}

public int getNum(int left,int right){
int result=0;
if(left>=right)
return 1;
for(int i=left;i<=right;i++){
int leftnum=getNum(left, i-1);
int rightnum=getNum(i+1, right);
result+=leftnum*rightnum;
}
return result;
}

public static int numTrees(int n) {
int[] cnt = new int[n+1];
cnt[0] = 1;
cnt[1] = 1;
for(int i=2; i<=n; i++){
for(int j=0; j<=i-1; j++){
cnt[i] += cnt[j] * cnt[i-1-j];
}
}
return cnt[n];
}

#### 95 Unique Binary Search Trees II

2015-11-28 12:59:53

#### Unique Binary Search Trees -- LeetCode

2014-04-30 06:16:29

#### 96 Unique Binary Search Trees

2015-11-25 21:04:50

#### leetcode 95. Unique Binary Search Trees II | Java最短代码实现

2016-03-09 20:43:19

#### 【LeetCode】【Python题解】Unique Binary Search Trees

2014-07-24 10:27:20

#### LeetCode | Unique Binary Search Trees II

2013-12-23 14:58:40

#### LeetCode || Unique Binary Search Trees

2014-04-09 23:16:41

#### LeetCode(95)Unique Binary Search Trees II

2014-01-13 05:20:18

#### [C++]LeetCode: 92 Unique Binary Search Trees II

2015-01-14 15:25:46

#### LeetCode96:Unique Binary Search Trees

2015-06-24 15:56:10