Skip to interview prep
Apexed

93. Right Side View of a Binary Tree

MediumTrees

Imagine standing to the right of a binary tree. Return the values you can see, from the top level to the bottom: the rightmost node of each level.

A binary tree is passed as TreeNode objects with val, left and right. The tests write trees in level order, with null for a missing child.

Example 1

Input: root = [1, 2, 3, null, 5, null, 4]
Output: [1, 3, 4]

Constraints

  • 0 ≤ nodes ≤ 100

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 4 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

Right Side View of a Binary Tree — coding interview practice — Apexed