Gamified challenge · Breadth-first search
The building is a binary tree given as tree (a TreeNode root; examples are in level order with null for a missing room). Looking from the right side, on every floor you see only the rightmost room; everything to its left is hidden behind it.
Write rightView(tree) (Python: right_view(tree)) that returns the numbers you see, top floor first. An empty tree returns an empty list.
For [1, 2, 3, null, 5, null, 4] the answer is [1, 3, 4]. For [1, 2, 3, 4] it is [1, 3, 4]: the left wing reaches lower, so its bottom room shows through.
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google