Phase 1: Look at the tree and predict the queue state after processing.

BFS gives you 3920157. Perfect order. But the problem wants 3,920,157. Which nodes belong to which level? The queue knows -- but only if you ask at the right moment.

Given a binary tree, return the values of its nodes grouped by depth level -- top to bottom, left to right within each level. Then: return only the rightmost visible node at each level (what you'd see standing on the right side looking left).

FIG. 1 — THE TREE AND BOTH TARGET OUTPUTS
Input
Level Order

[[3], [9, 20], [15, 7]]

Right Side View

[3, 20, 7]

— Level Order groups by level; Right Side View takes the last per level —

Plain BFS visits every node in the right order -- 3, 9, 20, 15, 7. But it gives you a FLAT list. The queue erases the level boundary the moment it processes the next node. Somehow you need to know “the level just ended” while the queue keeps growing.

FIG. 2 — PREDICT THE QUEUE

I'm running plain BFS. After processing the root (3), the queue holds 920. I process 9 and 20. What does the queue hold now -- and how would you know the level just ended?

— What happens after processing root? —