96. Unique Binary Search Trees
Difficulty: Medium
Topics: Dynamic Programming, Tree
Similar Questions:
Problem:
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
Solutions:
class Solution {
public:
int numTrees(int n) {
map<pair<int, int>, int> cache;
return helper(1, n, cache);
}
int helper(int left, int right, map<pair<int, int>, int>& cache) {
if (left > right) return 1; // return 1!
if (left == right) return 1;
auto range = make_pair(left, right);
if (cache.count(range) != 0) return cache[range];
int count = 0;
for (int num = left; num <= right; ++num) {
count += helper(left, num - 1, cache) * helper(num + 1, right, cache);
}
cache[range] = count;
return count;
}
};