149. Binary Tree Right Side View

Medium · Binary Tree

Given a binary tree, find the right side view — the values of the rightmost node visible at each depth level when looking at the tree from the right side.

The input is an array representing a binary tree in level-order (breadth-first) format, where `null` represents a missing node. Your function should return an array of the values of the rightmost node at each depth, ordered from top to bottom.

For example, in a tree with root 1, left child 2, and right child 3, the right side view is [1, 3] because 1 is visible at depth 0 and 3 is the rightmost node at depth 1.

Examples

Example 1
Input: [1,2,3,null,5,null,4]
Output: [1,3,4]
Explanation: Rightmost at each depth
Example 2
Input: [1,null,3]
Output: [1,3]
Explanation: No left subtree at depth 1

Constraints