An open API service indexing awesome lists of open source software.

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

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