Skip to content
DeepForgeby svx
All problems
EasyData Structuresds-099

Tree Right View

Problem

Return the right-side view of a binary tree, from top to bottom.

tree is a level-order list with None marking missing nodes; children of index i are at 2*i + 1 and 2*i + 2. The right view takes the rightmost existing node at each depth.

Return the list of visible values.

Hint

Process each level left to right and keep the last real value seen.

Test cases

case 1
in: [[1,2,3,null,5,null,4]]
expected: [1,3,4]
case 2
in: [[1,2]]
expected: [1,2]
case 3
in: [[]]
expected: []
case 4
in: [[1,2,3,4]]
expected: [1,3,4]
Starter codePython
def right_view(tree):
    # Your code here
    pass

4 test cases · run your code against them in the editor