mediumTreesAmazonBloombergGoogleFacebook
Wave Broadcast Scanner
A wireless antenna broadcasts a spatial ping. The network analyzer logs the propagation waves horizontal level-by-level.
Given the root of a binary tree, return the level order traversal of its nodes' values (i.e., from left to right, level by level).
Choose approach
3
wave
9
20
15
7
1/12
Narrationstep 1 of 12
Init level traversal depth 0. Count 1 active broadcast sources.
Variables
depth=0
queue=3
currentLevel=[]
result=[]
How to think about it
- Place root inside a FIFO Queue.
- At every level loop, poll size elements from the queue.
- Pull node values, and queue their valid left / right descendants.
- Push completed levels array into global tracker.
Edge cases to catch in interview
- Empty target tree -> returns vacant array.
- Highly unbalanced nodes straight down -> returns list of single assemblies.
Examples
Example 1
[[3], [9,20], [15,7]]{"arr":[3,9,20,null,null,15,7]}
Example 2
[[1]]{"arr":[1]}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed