← DiffPush

Left or Right View of Binary Tree

Standard Bar Binary Trees · Medium Problems O(n) · O(n)

The Balcony Spotlight Pass

A theater's rigging grid hangs as a binary tree of light battens, and the lighting director watches from the right balcony. On every tier, battens farther left are blocked by the first one in the line of sight, so only the rightmost batten of each tier shows. The cue sheet lists the visible batten numbers from the top tier down.

Input: The root of a binary tree given as a level-order array where null marks a missing child.

Output: Return an array: the rightmost node's value on each level, ordered top to bottom.

Constraints

Examples

Example 1

Input: {"tree":[1,2,3,null,5,null,4]}
Output: [1,3,4]
Tier two shows 3 (its rightmost), and tier three shows 4 even though 5 exists on the left.

Example 2

Input: {"tree":[1,null,3]}
Output: [1,3]
Each tier has exactly one visible batten.

Solve this in your browser →

Also on LeetCode ↗