All problems
1161EasyTreeDepth-First SearchBreadth-First SearchBinary Tree

Weights on the Tips of First Slots

Tracked in this browser only
Write code

Trains the technique from

LeetCode 404Sum of Left Leaves

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 mobile hangs from a single top weight. Every weight holds at most two weights below it in a first and a second slot, and either slot may be empty. Each weight shows a number.

A flat list is read level by level: its first entry is the top weight's number, and reading left to right, every entry that is not null claims the next two unused positions as its first and second slot in that order, while null marks an empty slot and claims no positions of its own.

A weight is a tip when both of its slots are empty. Return the total of the numbers on the tips that hang in a first slot.

Examples

Example 1

Input
mobile = [1, 2, 3, 4, 5, 6, 7]
Output
10

The four weights at the bottom are the tips. Two of them hang in a first slot, showing 4 and 6, so the total is 10.

Example 2

Input
mobile = [1, null, 2]
Output
0

The one weight below the top hangs in a second slot, so nothing counts.

Example 3

Input
mobile = [1, 2]
Output
2

The single weight below the top hangs in a first slot with nothing under it.

Constraints

  • 1 <= mobile.length <= 2000
  • The mobile holds between 1 and 1000 weights.
  • -1000 <= mobile[i] <= 1000
  • The first entry of the list is not null.

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 sum_of_left_leaves(mobile: list) -> int:
Java
public int sumOfLeftLeaves(Integer[] mobile)
September 7
Apply