https://github.com/emahtab/unique-binary-search-trees
Count the total number of BST possible with n numbers
https://github.com/emahtab/unique-binary-search-trees
binary-search-tree dynamic-programming leetcode problem-solving
Last synced: 9 days ago
JSON representation
Count the total number of BST possible with n numbers
- Host: GitHub
- URL: https://github.com/emahtab/unique-binary-search-trees
- Owner: eMahtab
- Created: 2020-02-08T08:47:09.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-02-08T08:49:27.000Z (over 5 years ago)
- Last Synced: 2025-03-28T01:28:52.308Z (7 months ago)
- Topics: binary-search-tree, dynamic-programming, leetcode, problem-solving
- Homepage:
- Size: 1000 Bytes
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Unique Binary Search Trees
## https://leetcode.com/problems/unique-binary-search-trees
Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n?
```
Example:
Input: 3
Output: 5
Explanation:
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
```
## Implementation :
```java
class Solution {
public int numTrees(int n) {
int[] G = new int[n + 1];
G[0] = 1;
G[1] = 1;
for (int i = 2; i <= n; ++i) {
for (int j = 1; j <= i; ++j) {
G[i] += G[j - 1] * G[i - j];
}
}
return G[n];
}
}
```
# References :
1. https://www.youtube.com/watch?v=GgP75HAvrlY
2. https://leetcode.com/articles/unique-binary-search-trees