ExamAdda Logo

Binary Tree Right Side View

Easy

Given the root of a binary tree, imagine yourself 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.

Example 1

Input

n = 7
root = [1,2,3,-1,5,-1,4]

Output

[1,3,4]

Explanation

From the right side, the visible node at each level is:

1 → 3 → 4

Example 2

Input

n = 3
root = [1,-1,3]

Output

[1,3]

Explanation

There is one visible node at each level, so the right-side view is [1,3].

Constraints

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

Hints:

Hint 1

Process the tree level by level.

Hint 2

For each level, the last node processed 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 19, 2026

Last updated

Aug 20, 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