Unique Binary Search Trees II
Real-world version: Every Search Tree Over the First n LabelsThe statement here is our own: the same inputs, outputs and technique.
A search tree over the labels 1 through n holds each label once, and every label is larger than everything in its first slot's subtree and smaller than everything in its second slot's subtree.
Read a tree by visiting its top label, then everything in its first slot, then everything in its second slot, which gives one sequence of labels per tree and tells the trees apart.
Return the readings of all the distinct search trees over those labels, listed in increasing dictionary order.
Examples
Example 1
- Input
- n = 2
- Output
- [[1, 2], [2, 1]]
Either label may sit at the top. With the smaller on top the larger hangs in its second slot, and the other way round the smaller hangs in the first slot.
Example 2
- Input
- n = 4
- Output
- [[1, 2, 3, 4], [1, 2, 4, 3], [1, 3, 2, 4], [1, 4, 2, 3], [1, 4, 3, 2], [2, 1, 3, 4], [2, 1, 4, 3], [3, 1, 2, 4], [3, 2, 1, 4], [4, 1, 2, 3], [4, 1, 3, 2], [4, 2, 1, 3], [4, 3, 1, 2], [4, 3, 2, 1]]
Fourteen search trees hold four labels, and their top-first readings sorted give this list.
Example 3
- Input
- n = 5
- Output
- [[1, 2, 3, 4, 5], [1, 2, 3, 5, 4], [1, 2, 4, 3, 5], [1, 2, 5, 3, 4], [1, 2, 5, 4, 3], [1, 3, 2, 4, 5], [1, 3, 2, 5, 4], [1, 4, 2, 3, 5], [1, 4, 3, 2, 5], [1, 5, 2, 3, 4], [1, 5, 2, 4, 3], [1, 5, 3, 2, 4], [1, 5, 4, 2, 3], [1, 5, 4, 3, 2], [2, 1, 3, 4, 5], [2, 1, 3, 5, 4], [2, 1, 4, 3, 5], [2, 1, 5, 3, 4], [2, 1, 5, 4, 3], [3, 1, 2, 4, 5], [3, 1, 2, 5, 4], [3, 2, 1, 4, 5], [3, 2, 1, 5, 4], [4, 1, 2, 3, 5], [4, 1, 3, 2, 5], [4, 2, 1, 3, 5], [4, 3, 1, 2, 5], [4, 3, 2, 1, 5], [5, 1, 2, 3, 4], [5, 1, 2, 4, 3], [5, 1, 3, 2, 4], [5, 1, 4, 2, 3], [5, 1, 4, 3, 2], [5, 2, 1, 3, 4], [5, 2, 1, 4, 3], [5, 3, 1, 2, 4], [5, 3, 2, 1, 4], [5, 4, 1, 2, 3], [5, 4, 1, 3, 2], [5, 4, 2, 1, 3], [5, 4, 3, 1, 2], [5, 4, 3, 2, 1]]
Forty-two search trees hold five labels.
Constraints
- 1 <= n <= 8
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def generate_trees(n: int) -> list[list[int]]:public List<List<Integer>> generateTrees(int n)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 8 test cases: 8 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Unique Binary Search TreesReal-world version: Shapes of the Docket RegistryMedium
- Maximum Sum BST in Binary TreeReal-world version: The Heaviest Sorted Section of a RigHard
- Convert Sorted Array to Binary Search TreeReal-world version: Balanced Marker IndexEasy
- Two Sum IV - Input is a BSTReal-world version: Two Calibration Cards to a TargetEasy