95. Unique Binary Search Trees II
Unique Binary Search Trees II (LeetCode 95) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Dynamic Programming (DP) Patterns pattern, in the Catalan Numbers subpattern.
How to solve Unique Binary Search Trees II
Unique Binary Search Trees II is a Catalan Numbers DP problem, part of Dynamic Programming (DP) Patterns. Solve problems involving Catalan numbers: unique BSTs, parentheses combinations, polygon triangulation.
Work towards the solution in these steps:
- Consider how recursive approaches can help in constructing complex tree structures by combining smaller subtrees.
- Think about selecting each number from 1 to n as a root, and how left and right subtrees can be formed from the remaining numbers.
- Explore how you can recursively generate all unique BSTs for ranges of values, and then combine these subtrees to form larger trees.
The reference solution runs in O(C_n * n^2) time and O(C_n * n) space.
The Unique Binary Search Trees II solution and editorial walks through the full approach with reference code.
Problem statement
Given an integer n, return all the structurally unique BST's (binary search trees), which has exactly n nodes of unique values from 1 to n. Return the answer in any order.
Example 1:
Input: n = 3 Output: [[1,null,2,null,3],[1,null,3,2],[2,1,3],[3,1,null,null,2],[3,2,null,1]]
Example 2:
Input: n = 1 Output: [[1]]
Constraints:
- 1 <= n <= 8
Topics and companies
Topics: Dynamic Programming, Backtracking, Tree, Binary Search Tree, Binary Tree.
Reported in interviews at Amazon, Google.
How to practise Unique Binary Search Trees II on Thita.ai
Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(C_n * n^2) time and O(C_n * n) space.
Open Unique Binary Search Trees II in the code editor, or read the Unique Binary Search Trees II editorial for a worked solution with its approach and complexity analysis.
Where Unique Binary Search Trees II sits in the DSA pattern sheet
- Dynamic Programming (DP) Patterns pattern guide
- Catalan Numbers subpattern
- The full DSA patterns sheet
Problems related to Unique Binary Search Trees II
Other problems that use the same Catalan Numbers and Dynamic Programming (DP) Patterns techniques:
- Different Ways to Add Parentheses — Medium, same subpattern
- Unique Binary Search Trees — Medium, same subpattern
- Best Time to Buy and Sell Stock — Easy, same pattern
- Best Time to Buy and Sell Stock II — Medium, same pattern
- Best Time to Buy and Sell Stock III — Hard, same pattern
- Best Time to Buy and Sell Stock IV — Hard, same pattern
Unique Binary Search Trees II FAQ
What is the LeetCode number of Unique Binary Search Trees II?
Unique Binary Search Trees II is LeetCode problem 95, rated Medium.
What pattern is used to solve Unique Binary Search Trees II?
Unique Binary Search Trees II is a Catalan Numbers DP problem. It belongs to the Dynamic Programming (DP) Patterns pattern, in the Catalan Numbers subpattern.
What is the time complexity of Unique Binary Search Trees II?
The reference solution runs in O(C_n * n^2) time and O(C_n * n) space.
Which companies ask Unique Binary Search Trees II?
Unique Binary Search Trees II has been reported in interviews at Amazon, Google.