Leetcode 95. Unique Binary Search Trees II
Given an integer n
, generate all structurally unique BST’s (binary search trees) that store values 1 to n
.
Before proceeding, let’s clarify a few points to ensure we understand the problem requirements:
n
?
Let’s assume:
n
is an integer where (1 \leq n \leq 8), making it feasible to generate and return all unique BSTs.Let’s assume the definition of TreeNode is as below (commonly used in LeetCode problems):
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) { this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
Recursion: We’ll use recursion to solve this problem. The idea is to consider each number from 1 to n
as the root and recursively generate all possible left and right subtrees.
Divide and Conquer: When a number i
is chosen as the root, the numbers from 1 to i-1
form the left subtree, and the numbers from i+1
to n
form the right subtree. We’ll recursively generate all unique subtrees for each possible root.
Merging Trees: For each possible left subtree and each possible right subtree, we create a new tree with the current number as the root.
Base Case: If start > end
, return a list containing null
(indicating no subtree).
Here’s how to implement this in Java:
import java.util.ArrayList;
import java.util.List;
public class Solution {
public static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) { this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
public List<TreeNode> generateTrees(int n) {
if (n == 0) return new ArrayList<>();
return generateTrees(1, n);
}
private List<TreeNode> generateTrees(int start, int end) {
List<TreeNode> allTrees = new ArrayList<>();
if (start > end) {
allTrees.add(null);
return allTrees;
}
for (int i = start; i <= end; i++) {
List<TreeNode> leftTrees = generateTrees(start, i - 1);
List<TreeNode> rightTrees = generateTrees(i + 1, end);
for (TreeNode left : leftTrees) {
for (TreeNode right : rightTrees) {
TreeNode currentTree = new TreeNode(i);
currentTree.left = left;
currentTree.right = right;
allTrees.add(currentTree);
}
}
}
return allTrees;
}
public static void main(String[] args) {
Solution solution = new Solution();
List<TreeNode> result = solution.generateTrees(3);
// Add code to print the result if needed
}
}
This problem has a catalan number growth rate because it involves generating all possible BSTs:
n
nodes is given by the nth Catalan number: ( C_n = \frac{1}{n+1} \binom{2n}{n} ).n
.So, the time complexity for generating all unique BSTs is (O(C_n)), which is the nth Catalan number.
The solution utilizes recursive tree generation with divide and conquer methodology to generate all unique BSTs for a given number n. The resulting trees are returned as a list of TreeNode objects.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?