Fetching latest headlines…
Right View of a Binary Tree
NORTH AMERICA
🇺🇸 United StatesJuly 4, 2026

Right View of a Binary Tree

4 views0 likes0 comments
Originally published byDev.to

Problem Statement

Given the root of a binary tree, return the nodes visible when the tree is viewed from the right side.

Only the last node at each level is visible.

Brute Force Intuition

In an interview, you can explain it like this:

Perform a level-order traversal (BFS). For every level, record the last node encountered.

Since BFS already processes nodes level by level, identifying the last node is straightforward.

Complexity

  • Time Complexity: O(N)
  • Space Complexity: O(N)

Moving Towards the Optimal Approach

Notice that during BFS:

Each Level

↓

Processed Together

If we know:

Level Size

then the last node processed at that level is exactly the right view.

Pattern Recognition

Whenever you see:

  • Left View
  • Right View
  • Level Wise Traversal

Think:

Level Order Traversal (BFS)

Key Observation

For every level:

Last Node

↓

Right View

Continue BFS until all levels are processed.

Optimal Approach

Step 1

Push the root into the queue.

Step 2

Process one level at a time.

size = queue.size();

Step 3

The last node:

i == size - 1

belongs to the right view.

Step 4

Push:

Left Child

↓

Right Child

into the queue.

Optimal Java Solution

class Solution {

    public List<Integer> rightSideView(TreeNode root) {

        List<Integer> ans = new ArrayList<>();

        if (root == null)
            return ans;

        Queue<TreeNode> q = new LinkedList<>();

        q.offer(root);

        while (!q.isEmpty()) {

            int size = q.size();

            for (int i = 0; i < size; i++) {

                TreeNode curr = q.poll();

                if (i == size - 1)
                    ans.add(curr.val);

                if (curr.left != null)
                    q.offer(curr.left);

                if (curr.right != null)
                    q.offer(curr.right);
            }
        }

        return ans;
    }
}

Dry Run

         1
       /   \
      2     3
     / \     \
    4   5     6

Level 1

1

Last Node:

1

Level 2

2 3

Last Node:

3

Level 3

4 5 6

Last Node:

6

Final Answer:

[1,3,6]

Why BFS Works?

BFS processes nodes level by level.

Since nodes are visited from left to right, the last node processed at every level is exactly the node visible from the right side.

Complexity Analysis

Metric Complexity
Time Complexity O(N)
Space Complexity O(N)

Interview One-Liner

Perform a level-order traversal and record the last node encountered at every level.

Pattern Learned

Level Order

↓

Last Node

↓

Right View

Similar Problems

  • Right View of Binary Tree
  • Left View of Binary Tree
  • Top View
  • Bottom View
  • Zigzag Level Order Traversal

Memory Trick

Think:

One Level

↓

Last Node

↓

Answer

Mental Model

Queue

↓

Process One Level

↓

Pick Last Node

↓

Continue

Whenever you hear:

"Right View of Binary Tree"

your brain should immediately think:

Level Order Traversal + Last Node of Every Level

Comments (0)

Sign in to join the discussion

Be the first to comment!