Skip to content

Commit 2e233ee

Browse files
committed
add 096
1 parent 5b8349f commit 2e233ee

File tree

8 files changed

+379
-1
lines changed

8 files changed

+379
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ Idx | Date | Question | Python| Java | Domain | Tag | Difficulty | Remark
3636
030|20200329|[1. Two Sum](https://leetcode.com/problems/two-sum/)|[001p](https://github.com/shishishu/leetcode-python-java/blob/master/ipynb_files/001_Two_Sum.ipynb)|[001j](https://github.com/shishishu/leetcode-python-java/blob/master/java_codes/001/Solution.java)|Array|HashTable|Easy
3737
031|20200329|[653. Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/)|[653p](https://github.com/shishishu/leetcode-python-java/blob/master/ipynb_files/653_Two_Sum_IV_-_Input_is_a_BST.ipynb)|[653j](https://github.com/shishishu/leetcode-python-java/blob/master/java_codes/653/Solution.java)|BST| |Easy
3838
032|20200406|[226. Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/)|[226p](https://github.com/shishishu/leetcode-python-java/blob/master/ipynb_files/226_Invert_Binary_Tree.ipynb)|[226j](https://github.com/shishishu/leetcode-python-java/blob/master/java_codes/226/Solution.java)|Tree|Recursive/Iterative|Easy
39-
033|20200406|[110. Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/)|[110p](https://github.com/shishishu/leetcode-python-java/blob/master/ipynb_files/110_Balanced_Binary_Tree.ipynb)|[110j](https://github.com/shishishu/leetcode-python-java/blob/master/java_codes/110/Solution.java)|Tree|Recursive|Easy
39+
033|20200406|[110. Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/)|[110p](https://github.com/shishishu/leetcode-python-java/blob/master/ipynb_files/110_Balanced_Binary_Tree.ipynb)|[110j](https://github.com/shishishu/leetcode-python-java/blob/master/java_codes/110/Solution.java)|Tree|Recursive|Easy
40+
034|20200406|[96. Unique Binary Search Trees](https://leetcode.com/problems/unique-binary-search-trees/)|[096p](https://github.com/shishishu/leetcode-python-java/blob/master/ipynb_files/096_Unique_Binary_Search_Trees.ipynb)|[096j](https://github.com/shishishu/leetcode-python-java/blob/master/java_codes/096/Solution.java)|BST|DP|Medium

images/096_Q.PNG

38.5 KB
Loading

images/096_S1.PNG

9.82 KB
Loading

images/096_S2.PNG

23.5 KB
Loading

images/096_S3.PNG

9.64 KB
Loading

ipynb_files/096_Unique_Binary_Search_Trees.ipynb

Lines changed: 346 additions & 0 deletions
Large diffs are not rendered by default.

java_codes/096/Solution.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// DP
2+
class Solution {
3+
public int numTrees(int n) {
4+
int[] G = new int[n + 1];
5+
G[0] = G[1] = 1;
6+
for (int i=2; i <= n; i++){
7+
for (int j=1; j<=i; j++){
8+
G[i] += G[j - 1] * G[i - j];
9+
}
10+
}
11+
return G[n];
12+
}
13+
}
14+
15+
// Recursive
16+
class Solution {
17+
public int numTrees(int n) {
18+
return subTrees(1, n);
19+
}
20+
public int subTrees(int lo, int hi){
21+
if (lo >= hi){
22+
return 1;
23+
}
24+
int total = 0;
25+
for (int i=lo; i<=hi; i++){
26+
total += subTrees(lo, i - 1) * subTrees(i + 1, hi);
27+
}
28+
return total;
29+
}
30+
}

java_codes/java_codes.iml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<sourceFolder url="file://$MODULE_DIR$/653" isTestSource="false" />
3838
<sourceFolder url="file://$MODULE_DIR$/226" isTestSource="false" />
3939
<sourceFolder url="file://$MODULE_DIR$/110" isTestSource="false" />
40+
<sourceFolder url="file://$MODULE_DIR$/096" isTestSource="false" />
4041
</content>
4142
<orderEntry type="inheritedJdk" />
4243
<orderEntry type="sourceFolder" forTests="false" />

0 commit comments

Comments
 (0)