We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 7c84db5 commit 3f3f1b9Copy full SHA for 3f3f1b9
096. Unique Binary Search Trees.java
@@ -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