Unique Binary Search Trees II
A medium-tier problem at 60% community acceptance, tagged with Dynamic Programming, Backtracking, Tree. Reported in interviews at Amazon and 6 others.
Unique Binary Search Trees II is a medium-difficulty problem that shows up in interviews at Amazon, Apple, Meta, Google, Microsoft, Bloomberg, and Uber. You're asked to generate all structurally unique BSTs with n nodes. The 60% acceptance rate tells you most candidates either nail it or miss the recursion pattern entirely. There's no way to brute-force this cleanly without understanding how to build trees bottom-up. If this problem hits your live assessment and you blank on the recursive construction, StealthCoder solves it in seconds, invisible to the proctor.
Companies that ask "Unique Binary Search Trees II"
Unique Binary Search Trees II is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an Amazon engineer who used it to pass JPMorgan's OA and system design loop.
Get StealthCoderThe trick is recognizing that all valid BSTs with n nodes come from picking each number as root, then recursively building all valid left and right subtrees from smaller ranges. Most candidates try iterative approaches or fail to connect the recursive structure to the final result list. The gotcha: you're not counting trees, you're generating actual tree objects, which means you need to understand how to clone and link subtrees across multiple recursive calls. This combines Dynamic Programming (overlapping subproblems) and Backtracking (exploring all valid structures). The problem tests whether you see the pattern of dividing the problem space by root choice. StealthCoder is the hedge if you freeze on the recursive formula during the assessment.
Pattern tags
You know the problem.
Make sure you actually pass it.
Unique Binary Search Trees II recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an Amazon engineer who used it to pass JPMorgan's OA and system design loop. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Unique Binary Search Trees II interview FAQ
How is this different from the count-only version?+
Counting trees uses DP memoization with a single integer result. Here you must return actual tree nodes. The recursion is similar, but you're building and linking Node objects across all left-right subtree combinations. That's why most candidates get stuck: they solve the math but fail the construction.
Why does this appear at Meta, Google, and Amazon so often?+
It tests whether you understand recursive tree building, think in terms of subproblems, and handle object creation correctly under multiple branches. These are core skills for backend interview loops at scale. The 7-company ask list confirms it's a top-tier screen question.
Is the obvious recursive approach actually optimal?+
Yes. The recursive divide-and-conquer by root is the expected solution. The catch is efficient memoization and proper tree-cloning across branches. Candidates often write correct logic but time out because they don't cache results from overlapping subproblems.
What's the main pitfall most candidates hit?+
Forgetting that each recursive call returns a list of trees, not a single tree. They try to link nodes directly instead of iterating all combinations of left and right subtrees. This is a backtracking mindset failure, not a tree knowledge gap.
Does this problem require advanced tree knowledge?+
No. It assumes you know BST properties and can create nodes. The hard part is the recursive formula: for root i, all valid left trees use nodes less than i, all valid right trees use nodes greater than i. Once you see it, the code flows.
Want the actual problem statement? View "Unique Binary Search Trees II" on LeetCode →