Problem Description
Given an integer n, return the number of structurally unique BST's (binary search trees) that can be built with n nodes, where each BST contains unique values from 1 to n.
Key Insights
- The problem is equivalent to computing the nth Catalan number.
- A recurrence relation can be derived: G(n) = Sum (G(i-1) * G(n-i)) for i from 1 to n.
- Dynamic Programming is an ideal approach to avoiding redundant calculations.
- The base case is the empty tree, which counts as 1.
Space and Time Complexity
Time Complexity: O(n^2) due to nested loops for calculating each dp state. Space Complexity: O(n) for storing the dp array.
Solution
We use Dynamic Programming where we define a dp array such that dp[i] represents the number of unique BST's possible with i nodes. Start with dp[0] = 1 (the empty tree). For each number of nodes from 1 to n, consider each node as the root, splitting the tree into a left subtree with (root-1) nodes and a right subtree with (n-root) nodes. The product of these gives the number of trees for that root, and we sum these over all possible roots. This builds the solution for dp[n] using smaller subproblems.
Code Solutions
Below are code solutions with line-by-line comments in Python, JavaScript, C++, and Java.