Binary tree right side view

Write codeMedium25 min

Your task

A binary tree is given in level-order array form where null children are represented by the value None... but to keep inputs JSON-friendly, it is given as a flat list using -1 as the null sentinel (all real node values are non-negative). Index 0 is the root; for a node at index i its left child is at 2i+1 and right at 2i+2 within the dense array (missing children are -1). Return the right-side view: the list of values visible from the right at each depth, top to bottom. The array has 0 to 2000 entries and the root, if present, is never -1.

Examples

right_side_view(tree: list[int]) -> list[int]
Example 1
Inputright_side_view([1, 2, 3, -1, 5, -1, 4])
Output[1, 3, 4]