Skip to main content

199. Binary Tree Right Side View

mediumAsked at LinkedIn

Return the values visible when looking at a binary tree from the right side — LinkedIn applies this BFS-by-level pattern to render hierarchical org-chart data, surfacing only the rightmost node at each reporting tier, a frequent warm-up before harder tree design questions.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Problem

Given the root of a binary tree, imagine standing on the right side of it. Return the values of the nodes you can see ordered from top to bottom (i.e., the last node at each level of the tree).

Constraints

  • The number of nodes in the tree is in the range [0, 100]
  • -100 <= Node.val <= 100

Examples

Example 1

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

Explanation: Level 0: 1 (rightmost=1), Level 1: 2,3 (rightmost=3), Level 2: 5,4 (rightmost=4).

Example 2

Input
root = [1,null,3]
Output
[1,3]

Approaches

1. DFS — right subtree first

DFS the tree visiting right children before left. Record each node value when visiting a new depth level for the first time. Since right is visited first, the first value recorded at each depth is the rightmost visible one.

Time
O(n)
Space
O(h) call stack
function rightSideViewDFS(root) {
  const result = [];
  function dfs(node, depth) {
    if (!node) return;
    if (depth === result.length) result.push(node.val); // first visit at this depth = rightmost
    dfs(node.right, depth + 1);
    dfs(node.left, depth + 1);
  }
  dfs(root, 0);
  return result;
}

Tradeoff:

2. BFS level-order — record last node each level — canonical

Process the tree level by level using a queue. At each level, the last element dequeued is the rightmost visible node. Push its value to the result. Intuitive and directly maps to the problem framing.

Time
O(n)
Space
O(w) where w is max tree width
function rightSideView(root) {
  if (!root) return [];
  const result = [];
  const queue = [root];
  while (queue.length) {
    const levelSize = queue.length;
    for (let i = 0; i < levelSize; i++) {
      const node = queue.shift();
      if (i === levelSize - 1) result.push(node.val); // last in level = rightmost
      if (node.left) queue.push(node.left);
      if (node.right) queue.push(node.right);
    }
  }
  return result;
}

Tradeoff:

LinkedIn-specific tips

LinkedIn uses this as an entry-level tree question to validate BFS-by-level fluency before moving to harder tree problems. Name the pattern explicitly: 'Level-order BFS with a level-size snapshot.' The DFS variant (right-first, record at first-visit per depth) is a neat trick but less intuitive for the interviewer — stick with BFS unless they ask for an O(h)-space alternative. Common follow-up: 'return the left side view' — the only change is to record the first node at each level instead of the last.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

Practice these live with InterviewChamp.AI

Drill Binary Tree Right Side View and other LinkedIn interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →