55. Binary Tree Right Side View

55. Binary Tree Right Side View

Medium
50.0%
3.2k
dfsbfsbinary-searchtreequeuerecursion
Google, Amazon, Netflix, Meta (Facebook), Apple, Microsoft, Uber, Bloomberg, Airbnb, Stripe, Adobe, Salesforce, LinkedIn, Oracle

A large-scale visualization system renders hierarchical data such as:

  • UI component trees
  • file systems
  • service dependency graphs

To improve clarity in dashboards, the system provides a “right-side projection” view of the tree — showing only the nodes visible when the structure is observed from the right side.

This is useful for:

  • summarizing deep hierarchies
  • monitoring layered systems
  • visualizing dominant nodes at each level

The system receives a binary tree and must return the list of nodes visible from the right side, ordered from top to bottom.

This is a common coding interview problem frequently asked in Google, Amazon, Netflix, Meta, Apple, Microsoft, Uber, and Bloomberg interviews.

Your Task

Given the root of a binary tree, return a list of values of the nodes you can see from the right side.

Input Format

  • root: root node of a binary tree

Each node:

TreeNode { int val; TreeNode left; TreeNode right;}

Output Format

  • A list/array of integers representing visible nodes from right view

Examples

Example 1:

Input: root = [1,2,3,null,5,null,4]
Output: [1,3,4]
Explanation: Rightmost node at each level → 1, 3, 4

Example 2:

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

Example 3:

Input: root = []
Output: []

Constraints

  • 0 ≤ number of nodes ≤ 100,000
  • -10^9 ≤ Node.val ≤ 10^9

Loading editor...

[1,2,3,null,5,null,4]
[1,3,4]

Console Output

Click "Run Code" or "Submit" to see results

Your code will be evaluated by AI with instant feedback