Why it works
- BFS takes a level left to right, and size says how many nodes it has, so i == size marks its rightmost node.
- Looking from the right, you see the rightmost node of each level, whichever subtree it hangs in: following right children alone would miss 8.
- Every level gets exactly one value, in order from the top, so the list is the view.
Complexity
Time O(n): each node enters and leaves the queue once. Memory O(w): the queue holds at most about two levels, w is the widest one.
nextNow solve it yourself, no hints.
Solve on LeetCode ↗