Skip to content

Commit 6ff1aa9

Browse files
solves unique binary search trees
1 parent 2738367 commit 6ff1aa9

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# LeetCode Algorithms
22

3-
![problems-solved](https://img.shields.io/badge/Problems%20Solved-460/2081-1f425f.svg)
4-
![problems-solved-java](https://img.shields.io/badge/Java-460/2081-1abc9c.svg)
3+
![problems-solved](https://img.shields.io/badge/Problems%20Solved-479/2081-1f425f.svg)
4+
![problems-solved-java](https://img.shields.io/badge/Java-479/2081-1abc9c.svg)
55
![problems-solved-python](https://img.shields.io/badge/Python-186/2081-1abc9c.svg)
66
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](CONTRIBUTING.md)
77
[![cp](https://img.shields.io/badge/also%20see-Competitve%20Programming-1f72ff.svg)](https://github.com/anishLearnsToCode/competitive-programming)
@@ -86,7 +86,7 @@
8686
| 93 | [Restore IP Addresses](https://leetcode.com/problems/restore-ip-addresses) | [![Java](assets/java.png)](src/RestoreIPAddresses.java) | |
8787
| 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal) | [![Java](assets/java.png)](src/BinaryTreeInorderTraversal.java) [![Python](assets/python.png)](python/binary_tree_inorder_traversal.py) | |
8888
| 95 | [Unique Binary Search Trees II](https://leetcode.com/problems/unique-binary-search-trees-ii) | [![Java](assets/java.png)](src/UniqueBinarySearchTreesII.java) | |
89-
| 96 | [Unique Binary Search Trees](https://leetcode.com/problems/unique-binary-search-trees) | | |
89+
| 96 | [Unique Binary Search Trees](https://leetcode.com/problems/unique-binary-search-trees) | [![Java](assets/java.png)](src/UniqueBinarySearchTrees.java) | |
9090
| 97 | [Interleaving String](https://leetcode.com/problems/interleaving-string) | | |
9191
| 98 | [Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree) | | |
9292
| 99 | [Recover Binary Search Tree](https://leetcode.com/problems/recover-binary-search-tree) | | |

src/UniqueBinarySearchTrees.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// T: O(n)
2+
// S: O(1)
3+
4+
public class UniqueBinarySearchTrees {
5+
public int numTrees(int n) {
6+
return catalanNumber(n);
7+
}
8+
9+
private int catalanNumber(int n) {
10+
long result = 1;
11+
for (int i = 2 ; i <= n ; i++) {
12+
result *= (4L * i - 2);
13+
result /= i + 1;
14+
}
15+
return (int) result;
16+
}
17+
}

0 commit comments

Comments
 (0)