Search in a Binary Search Tree
Real-world version: Pulling A Branch Out Of A Code IndexThe statement here is our own: the same inputs, outputs and technique.
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 starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def index_branch(index: list, code: int) -> list:public List<Integer> indexBranch(Integer[] index, int code)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 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
- Convert Sorted Array to Binary Search TreeReal-world version: Balanced Marker IndexEasy
- Two Sum IV - Input is a BSTReal-world version: Two Calibration Cards to a TargetEasy
- Range Sum of BSTReal-world version: Auditing a Fare Band in the RegisterEasy
- Minimum Absolute Difference in BSTReal-world version: Closest Two Readings in the Pressure IndexEasy