Ninja techniqueπ₯· to ACE DSA Interviews.
dp[0] = dp[1] = 1.
i
from 2...n.
i
nodes. we can consider each of the node j
from 1...i
as the root node.i
nodes, the result is summation for all j
from 1...i
of dp[j-1] * dp[i-j]
. (Comparing to above solution dp[j-1] = numTrees(j-1) and dp[i-j]=numTrees(i-j)
)class Solution {
public:
int numTrees(int n) {
vector<int> dp(n+1);
dp[0] = dp[1] = 1;
for(int i = 2; i <= n; i++)
for(int j = 1; j <= i; j++)
dp[i] += dp[j-1] * dp[i-j];
return dp[n];
}
};
There are a few ways to solve this problem. I have explained all the solutions and approach to optimize it from Brute-Force to Dynamic Programming to solving using Catalan formula in: β [ Simple Solution w/ Explanation | Optimization from Brute-Force to DP](https://leetcode.com/problems/unique-binary-search-trees/discuss/1565543/Simple-Solution-w-Explanation-or-Optimization-from-Brute-Force-to-DP) |
( Time: O(3^N), Space: O(N) )
( Time: O(N^2), Space: O(N) )
( Time: O(N^2), Space: O(N) )
( Time: O(N), Space: O(1) )
( Time: O(N), Space: O(1) )
[C++ Easy & Clean Solution | Fastest | All (4+) Methods | Detailed Explanation & Comments](https://leetcode.com/problems/unique-binary-search-trees/discuss/1565544/C%2B%2B-Easy-and-Clean-Solution-or-Fastest-or-All-(3%2B)-Methods-or-Detailed-Explanation-and-Comments) |