96. Unique Binary Search Trees¶
Problem Description¶
LeetCode Problem 96: Given
an integer n
, return the number of structurally unique BST's (binary search trees)
which has exactly n
nodes of unique values from 1
to n
.
Clarification¶
-
Assumption¶
-
Solution¶
Approach 1: Dynamic Programming¶
For a given root node i
, the number of unique BSTs is the product of the
number of unique BSTs that from the left subtree based on subsequence
\(1, 2, \cdots, i - 1\) and the right subtree based on sequence \(i + 1, i + 2, \cdots, n\).
Based on this observation, we can solve the problem using dynamic programming. To do so, we can define two functions:
G(n)
: the number of unique BSTs that can be formed withn
nodes (i.e., length of a sequence).F(i, n)
: the number of unique BSTs that can be formed withi
as the root node with total \(n\) nodes (\(1 \leq i \leq n\)).
Based on above definitions, we can derive the following equations:
- \(G(n) = \sum_{i=1}^{n} F(i, n)\): sum of all unique BSTs with
i
as the root node wherei
ranges from 1 to n. - \(F(i, n) = G(i - 1) \cdot G(n - i)\): the number of unique BSTs with root
i
is the product of all unique number of BSTs formed withi - 1
nodes in the left subtree andn - i
nodes in the right subtree. - \(G(0) = 1\), \(G(1) = 1\): base case
So combine above equations together, we obtain:
Complexity Analysis of Approach 1¶
- Time complexity: \(O(n^2)\)
The outer loop runsn - 1
times and the inner loop runsi
times, wherei
ranges from2
ton
. The total number of iterations is \(\sum_{i=2}^{n} i = \frac{(n+2)(n-1)}{2}\). So the total time complexity is \(O(n^2)\). - Space complexity: \(O(n)\)
We use an arrayG
of sizen + 1
to store the number of unique BSTs for eachi
from0
ton
. So the space complexity is \(O(n)\).
Approach 2: Math¶
The sequence of G(n)
is known as the Catalan number. The n
th Catalan number can be
computed using the following formula:
where \(C_n\) is the n
th Catalan number, and \(\binom{2n}{n}\) is the binomial coefficient
defined as \(\binom{n}{k} = \frac{n!}{k!(n - k)!}\).
Complexity Analysis of Approach 2¶
- Time complexity: \(O(n)\)
The time complexity is dominated by the computation of the factorials. The computation of the factorials takes \(O(n)\) time. - Space complexity: \(O(1)\)
The space complexity is \(O(1)\) because we are using a constant amount of space to store the factorials.
Comparison of Different Approaches¶
The table below summarize the time complexity and space complexity of different approaches:
Approach | Time Complexity | Space Complexity |
---|---|---|
Approach - Dynamic Programming | \(O(n^2)\) | \(O(n)\) |
Approach - Math | \(O(n)\) | \(O(1)\) |