Skip to content

Commit 3f3f1b9

Browse files
authored
Create 096. Unique Binary Search Trees.java
1 parent 7c84db5 commit 3f3f1b9

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Diff for: 096. Unique Binary Search Trees.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
https://leetcode.com/problems/unique-binary-search-trees
2+
// Dynamic Programming!!!
3+
// see https://discuss.leetcode.com/topic/8398/dp-solution-in-6-lines-with-explanation-f-i-n-g-i-1-g-n-i
4+
5+
public class Solution {
6+
public int numTrees(int n) {
7+
int [] G = new int[n+1];
8+
G[0] = G[1] = 1;
9+
10+
for(int i=2; i<=n; ++i) {
11+
for(int j=1; j<=i; ++j) {
12+
G[i] += G[j-1] * G[i-j];
13+
}
14+
}
15+
16+
return G[n];
17+
}
18+
}

0 commit comments

Comments
 (0)