All problems
0990MediumTreeDepth-First SearchBreadth-First SearchBinary Tree

The Heaviest Load on Each Deck

Tracked in this browser only
Write code

Trains the technique from

LeetCode 515Find Largest Value in Each Tree Row

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 cargo rig is given as the flat list rig. It hangs from a single top joint, every joint carries at most two joints below it in a first and a second slot, and either slot may be empty.

A flat list is read level by level: its first entry is the top joint's load, 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 list that ends early leaves the remaining slots empty, and an empty list means no joints at all.

The decks of the rig are its levels: the top joint on its own, then the joints hanging directly off it, then the joints hanging off those, and so on.

Return the heaviest load on each deck, ordered from the top deck downwards. With no joints at all, return an empty list.

Examples

Example 1

Input
rig = [4, 9, 6, 2, 8, null, 7]
Output
[4, 9, 8]

The top deck is the joint loaded 4. Below it hang 9 and 6, the heavier being 9. The bottom deck holds 2, 8 and 7, and the second slot under the joint loaded 6 is empty, so the heaviest down there is 8.

Example 2

Input
rig = [5, 1, 4]
Output
[5, 4]

Two decks: the top joint on its own, then the pair below it, of which 4 is the heavier.

Example 3

Input
rig = [0, null, 3, null, 8]
Output
[0, 3, 8]

Each joint's first slot is empty, so every deck holds exactly one joint and its load is the heaviest there by default.

Constraints

  • 0 <= rig.length <= 30000
  • The rig holds between 0 and 10^4 joints.
  • -2^31 <= rig[i] <= 2^31 - 1
  • The first entry is not null unless the list is 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 largest_values(rig: list) -> list[int]:
Java
public int[] largestValues(Integer[] rig)
September 7
Apply