Asked at

Binary Tree Right Side View

Medium
Verified
TreeBFSDFS~20 min

Given the root of a binary tree, imagine standing on its right side. Return the values of the nodes you can see, ordered top to bottom.

The rightmost node at each depth is visible. A node is { val, left, right } with null children; the whole empty tree is null. The input arrives as the root node directly.

Examples

in{val:1, left:{val:2, right:{val:5}}, right:{val:3, right:{val:4}}}
out[1, 3, 4]

Looking from the right, the last node of each level is visible.

innull
out[]

An empty tree shows nothing.

Constraints

  • 0 ≤ number of nodes ≤ 100
  • -100 ≤ node.val ≤ 100

Get help

🔑

Sign in to solve

Sign in to write, run, and submit your solution — and to pick up where your iOS flow left off.