All problems
0135MediumTreeDepth-First SearchBreadth-First SearchBinary Tree

Tier Edge Relays

Tracked in this browser only
Write code

Trains the technique from

LeetCode 199Binary Tree Right Side View

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 broadcast network is wired as a binary tree of relays. Every relay may feed a left downstream relay and a right downstream relay, and relays at the same distance from the source form one tier. On each tier, the relay drawn furthest to the right is the one that reports that tier's health, so operations needs the reporting relay of every tier, from the source tier downward.

The tree arrives as network, a level-order listing: network[0] is the source relay's value, and the remaining entries give, tier by tier and left to right, the left feed then the right feed of each relay already listed. A null entry means that feed is absent; an absent feed contributes no entries of its own, and trailing null entries are omitted. An empty list means the network has no relays.

Return the values of the reporting relays, ordered from the source tier down to the deepest tier. Return an empty list when the network is empty. Note that a tier's rightmost relay need not descend from the previous tier's rightmost relay.

Examples

Example 1

Input
network = [8, 3, 11, null, 6, null, 14]
Output
[8, 11, 14]

The source reports its own tier. On the middle tier the right feed 11 sits furthest right, and on the bottom tier only 6 and 14 exist, so 14 reports.

Example 2

Input
network = [7, null, 4, 9, 2]
Output
[7, 4, 2]

The source has no left feed, so tier two holds only 4, whose two feeds 9 and 2 form tier three and 2 reports it.

Example 3

Input
network = [13, -7, 21, 4, -2, null, 30, null, null, 11]
Output
[13, 21, 30, 11]

Relay 21 has no left feed, so tier three is -2, 30 and the deepest tier is fed from the left branch alone, which makes 11 that tier's reporter.

Constraints

  • The number of relays is in the range [0, 100]
  • -100 <= network[i] <= 100 for every non-null entry
  • network is a valid level-order listing of a binary tree

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 tier_edge_relays(network: list) -> list[int]:
Java
public List<Integer> tierEdgeRelays(Integer[] network)
September 7
Apply