Univalued Binary Tree
Real-world version: Single Channel Relay NetworkThe statement here is our own: the same inputs, outputs and technique.
A broadcast operator runs a relay network shaped as a binary tree. One relay sits at the head of the network, and every relay feeds up to two relays below it, a first branch and a second branch, either of which may be empty. Each relay is tuned to a channel, written as a number.
The network arrives as channels, a level-order listing. The first entry is the channel of the relay at the head. After that, the listing gives the first branch then the second branch of each relay already listed, in the order those relays appear, writing null wherever a branch is empty. Slots below a null are never written down, and the trailing run of null entries is left off.
The network is easiest to maintain when it runs on one channel throughout. Return true when every relay in the network is tuned to the same channel, and false otherwise.
Examples
Example 1
- Input
- channels = [6, 6, 6, null, 6]
- Output
- true
The listing names four relays and every one of them sits on channel 6. The `null` is the empty first branch below the relay listed second, not a relay.
Example 2
- Input
- channels = [3, 3, null, 3, null, 3, null, 9]
- Output
- false
Each relay here feeds exactly one relay below it, giving a chain of channels 3, 3, 3, 3 and 9. The relay at the far end of the chain is on channel 9, which is not the channel at the head.
Example 3
- Input
- channels = [8]
- Output
- true
The network is a single relay on channel 8, and it agrees with itself.
Constraints
- The number of relays in the network is in the range [1, 100].
- 1 <= channels.length <= 200
- 0 <= channels[i] <= 99
- An entry of channels that is null marks an empty branch rather than a relay.
- channels[0] is never null.
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def is_single_channel(channels: list[int | None]) -> bool:public boolean isSingleChannel(Integer[] channels)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 18 test cases: 18 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's