Trains the technique from
LeetCode 95Unique Binary Search Trees IIThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
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.
Example 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
Fourteen search trees hold four labels, and their top-first readings sorted give this list.
Example 3
Forty-two search trees hold five labels.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def generate_trees(n: int) -> list[list[int]]:public List<List<Integer>> generateTrees(int n)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.