Trains the technique from
LeetCode 108Convert Sorted Array to Binary Search TreeThis 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 survey line carries pegs at known offsets from its datum, given as markers in strictly increasing order. An offset is signed: pegs behind the datum are negative.
Build a two-branch lookup tree over the pegs so that:
More than one tree can meet those three rules, so build the single tree pinned down by this rule: the peg at a node is the middle peg of the stretch of markers that node covers, and when that stretch holds an even number of pegs, take the lower of its two middle pegs.
Return the tree as a listing read off one depth at a time, left to right. Slot 0 holds the top node. Each node that appears in the listing claims the next two free slots for its branches, the left branch first and the right branch second. A slot holding null carries no node and claims no slots of its own. Leave off any null slots at the end of the listing.
Example 1
The stretch of four pegs has 1 and 4 in the middle, so the top node takes the lower one, 1. Its left branch covers -6 alone and its right branch covers 4 and 9, where 4 is the lower of that pair's middles and 9 sits above it. Both branches of the top node are one deep and two deep, within the allowed gap.
Example 2
Seven pegs give 11 as the middle peg, then 5 and 20 as the middles of the two halves, and the remaining four pegs fill the third depth. Every peg to the left of a node is below it and every peg to the right is above it.
Example 3
One peg means one node with no branches, and the two empty slots after it fall at the end of the listing, so they are left off.
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 balanced_marker_index(markers: list[int]) -> list[int | None]:public Integer[] balancedMarkerIndex(int[] markers)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.