Convert Sorted Array to Binary Search Tree
Real-world version: Balanced Marker IndexThe statement here is our own: the same inputs, outputs and technique.
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:
- every peg sits at exactly one node and every node carries exactly one peg;
- at any node, every peg in its left branch is below that node's own peg and every peg in its right branch is above it;
- the tree is depth balanced, meaning that at every node the depths of its two branches differ by at most one.
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.
Examples
Example 1
- Input
- markers = [-6, 1, 4, 9]
- Output
- [1, -6, 4, null, null, null, 9]
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
- Input
- markers = [2, 5, 7, 11, 14, 20, 23]
- Output
- [11, 5, 20, 2, 7, 14, 23]
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
- Input
- markers = [8]
- Output
- [8]
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.
Constraints
- 1 <= markers.length <= 10^4
- -10^4 <= markers[i] <= 10^4
- markers is given in strictly increasing order
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 balanced_marker_index(markers: list[int]) -> list[int | None]:public Integer[] balancedMarkerIndex(int[] markers)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 13 test cases: 13 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
- Construct Binary Tree from Preorder and Inorder TraversalReal-world version: Cave Survey RebuildMedium
- Convert Sorted List to Binary Search TreeReal-world version: Balanced Index Tree From A Reading ChainMedium
- Construct Binary Search Tree from Preorder TraversalReal-world version: Rebuild the Accession IndexMedium
- Balance a Binary Search TreeReal-world version: Rebalancing the Bin IndexMedium