ExamAdda Logo

Process one complete level at a time.

Easy

Given the root of a binary tree, imagine you are standing on the right side of the tree.

Return the values of the nodes that are visible from the right side, ordered from top to bottom.

For example:

     1   

    / \    

   2   3     

    \   \     

     5   4

 

The right-side view is:

[1, 3, 4]

 

At each level, we see the rightmost visible node.

Example 1

Input

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

Output

[1,3,4]

Explanation

The visible node at each level is:

Level 1 → 1 Level 2 → 3 Level 3 → 4

 

Therefore:

[1,3,4]

Example 2

Input

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

Output

[1,3,4,5]

Explanation

Although some visible nodes come from the left subtree, they are visible because there is no node from the right subtree blocking them.

Constraints

0 <= number of nodes <= 100
-100 <= Node.val <= 100

Hints:

Hint 1

Think about the tree level by level.

Hint 2

Using BFS, the last node processed at every level is the node visible from the right side.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 12, 2026

Last updated

Aug 12, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output