Trains the technique from
LeetCode 783Minimum Distance Between BST NodesThis 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 warehouse records the vertical clearance of every storage shelf, in millimetres, in a binary search tree keyed by the clearance: for any shelf in the tree, every clearance in the branch to its left is smaller and every clearance in the branch to its right is larger. No two shelves have the same clearance.
The tree arrives as clearances, a level-order listing. The first entry is the clearance at the top of the tree. After that, the listing gives the left branch then the right branch of each shelf already listed, in the order those shelves appear, writing null wherever a branch is empty. Slots below a null are never written down, and the trailing run of null entries is left off.
Two shelves are easy to mix up when their clearances are close together. Return the smallest difference in clearance between any two different shelves in the tree.
The tree always holds at least two shelves, so such a pair always exists.
Example 1
The tree holds three shelves, of clearance 10, 11 and 50. The pair 10 and 11 sit 1 millimetre apart, and no pair sits closer than that.
Example 2
Only two shelves are recorded, so the single available pair is 3 and 40 and the answer is their difference.
Example 3
Every shelf here hangs off the left branch of the one above it, giving clearances 9, 7, 5, 3 and 1. Several pairs sit 2 millimetres apart and none sits closer.
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 closest_clearance_gap(clearances: list[int | None]) -> int:public int closestClearanceGap(Integer[] clearances)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.