All problems
0459EasyTreeBinary Search TreeBinary Tree

Pulling A Branch Out Of A Code Index

Tracked in this browser only
Write code

Trains the technique from

LeetCode 700Search in a Binary Search Tree

This 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 depot keeps its part codes in an ordered index. The index has a head entry, every entry hangs at most two entries below it - one on its low side and one on its high side - and the ordering is strict: every code anywhere below an entry's low side is smaller than that entry's code, and every code anywhere below its high side is larger. No code appears twice.

The index arrives as the array index, written out level by level. index[0] is the head entry's code. The entries after it come in pairs, giving the low side and then the high side of each entry already written out, taken in the same order those entries were written. A side with nothing hanging on it is written null, and a null never claims a pair of its own. Trailing null entries are left off the end.

Return the branch headed by the entry whose code is code, written out in exactly that same convention: the matched code first, then the pairs for the entries of the branch in the order they are written, null for an empty side, and no trailing null entries. Return an empty list if no entry carries code.

Use the ordering to find the entry: the walk down should touch one entry per level, not the whole index.

Examples

Example 1

Input
index = [50, 22, 74, 9, 35, 61, 90, null, null, 30, null, null, null, 82], code = 22
Output
[22, 9, 35, null, null, 30]

The entry holding 22 has 9 on its low side and 35 on its high side; 9 hangs nothing, and 35 carries 30 on its low side and nothing on its high side.

Example 2

Input
index = [50, 22, 74, 9, 35, 61, 90, null, null, 30, null, null, null, 82], code = 90
Output
[90, 82]

The entry holding 90 carries 82 on its low side and nothing on its high side, and the blank for that empty high side falls at the end of the listing so it is left off.

Example 3

Input
index = [50, 22, 74, 9, 35, 61, 90, null, null, 30, null, null, null, 82], code = 47
Output
[]

No entry in the index holds 47.

Constraints

  • 1 <= number of entries <= 5000
  • 1 <= code held by an entry <= 10^7
  • 1 <= code <= 10^7
  • The codes held by the entries are distinct and obey the ordering above.
  • index[0] is a code, so the index is never empty

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def index_branch(index: list, code: int) -> list:
Java
public List<Integer> indexBranch(Integer[] index, int code)
September 7
Apply