All problems
0208EasyTreeDepth-First SearchBreadth-First SearchBinary Tree

Twin Splitter Cabinets

Tracked in this browser only
Write code

Trains the technique from

LeetCode 100Same Tree

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 site keeps its signal splitters in wall cabinets. One splitter sits at the feed point, and every splitter passes its signal on to at most two further splitters: one hanging off its port A and one hanging off its port B. Each splitter is engraved with a trim in decibels, which runs negative when the splitter cuts the level.

Because the harness passes plain JSON, a cabinet reaches you as an array of engraved trims. The first entry is the feed-point splitter. After that the entries arrive in pairs, giving the port A splitter and then the port B splitter of each splitter already listed, taken in the order they were listed. A port with nothing hanging off it is written null, and a null brings no pair of its own. An empty array means the cabinet is bare.

One consequence of that array form is worth stating: empty ports at the very end may be written out or left off, so a single cabinet can be described by more than one array.

You are given built, the cabinet already on the wall, and spare, a cabinet from the store room. Return true when an engineer could hang the spare in place of the built one, meaning the two hold splitters in exactly the same arrangement with the same trim engraved at every matching position, and false otherwise. Two bare cabinets are interchangeable.

Examples

Example 1

Input
built = [9, -2, 5, null, 7], spare = [9, -2, 5, 7]
Output
false

In the built cabinet the splitter trimmed -2 has nothing on port A and the splitter trimmed 7 on port B. In the spare, that same splitter carries 7 on port A instead, so the arrangements do not line up.

Example 2

Input
built = [4, -6], spare = [4, -6, null]
Output
true

Both cabinets hold a feed splitter trimmed 4 with a splitter trimmed -6 on port A and nothing on port B; the spare array simply writes that empty port out.

Example 3

Input
built = [12, 3, 8], spare = [12, 3, 9]
Output
false

Every position matches up, but the port B splitter is engraved 8 on the wall and 9 in the store room.

Constraints

  • The number of splitters in each cabinet is in the range [0, 100].
  • -10^4 <= trim <= 10^4
  • Every entry is either an integer trim or `null`; the first entry of a non-empty array is an integer.

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 twin_splitter_cabinets(built: list, spare: list) -> bool:
Java
public boolean twinSplitterCabinets(Integer[] built, Integer[] spare)
September 7
Apply