Trains the technique from
LeetCode 113Path Sum IIThis 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 of a mountain ridge records a network of marked waypoints. The topmost waypoint is the saddle, and every waypoint sends at most two marked forks further down the ridge, one bearing left and one bearing right. Each waypoint carries a signed height change in metres, positive where the ground rises onto it and negative where it falls onto it.
The survey arrives as waypoints, which lists the network one depth band at a time, left to right. Slot 0 holds the saddle. Each listed waypoint takes the next two unclaimed slots for its downhill forks, the left fork first and the right fork second. A slot holding null means no waypoint hangs there and it claims no slots of its own. Trailing null slots may be dropped from the end of the listing, and an empty listing means the survey found nothing.
A descent starts at the saddle, follows marked forks downhill, and stops at a waypoint with no fork leaving it. Its drop is the total of the height changes of every waypoint it touches, the saddle included.
Return every descent whose drop equals targetSum, each one given as the list of height changes in the order they are walked, from the saddle downward. The descents may come back in any order. Return an empty list when no descent matches.
Example 1
The saddle records 6. Its left fork reaches 4, whose left fork holds 2, and that waypoint has nothing below it: 6 + 4 + 2 = 12. The survey's other two descents finish at -1 and at 5, dropping 9 and 20.
Example 2
The ridge runs straight down: 3, then 5, then -2, then 4. Only the last of those has no fork leaving it, and the four height changes total 10.
Example 3
The saddle has no fork, so the single descent touches only it and drops 7, which matches.
Example 4
The survey lists no waypoints at all, so there is no descent to report.
Example 5
The saddle's left fork holds 1 with nothing below it, and 4 + 1 = 5. Its right fork holds 2 and still has two forks of its own: the one holding -1 brings that descent back to 5, while the one holding 6 finishes at 12.
The values you return may be in any order.
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 path_sum(waypoints: list[int | None], targetSum: int) -> list[list[int]]:public List<List<Integer>> pathSum(Integer[] waypoints, int targetSum)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.