Trains the technique from
LeetCode 938Range Sum of BSTThis 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 tramway fare machine files the day's takings in a sorted register. Each entry in the register records one fare in pence and hangs two branches beneath it, a lighter branch and a heavier branch. Every fare filed anywhere in an entry's lighter branch is strictly smaller than that entry's own fare, and every fare filed anywhere in its heavier branch is strictly larger. No two entries record the same fare.
The register reaches you as register, which lists the entries one level at a time from the top down and, inside a level, from left to right with the lighter branch before the heavier one. Slot 0 holds the top entry. Reading the listing from the front, each entry takes the next two slots that nothing has taken yet: the first belongs to its lighter branch and the second to its heavier branch. A slot holding null means that branch is empty, and an empty branch takes no slots of its own. null slots at the very end of the listing may be left out.
An auditor is reconciling one fare band. Return the total of every fare in the register that is at least low and at most high, counting both ends of the band. If no fare falls in the band, the total is 0.
Example 1
The register holds the fares 32, 15, 47, 8, 23, 39 and 55. Of those, 23, 32 and 39 are at least 20 and at most 45, and they come to 94.
Example 2
Two fares, 47 and 55, are at least 47 and at most 100, so the audit line comes to 102.
Example 3
The register holds one fare of 42, which is above the top of the band, so nothing is counted.
Example 4
The fares 45, 50, 70 and 85 all lie inside the band, and together they come to 250.
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 band_total(register: list[int | None], low: int, high: int) -> int:public int bandTotal(Integer[] register, int low, int high)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.