2Brute2Best
πŸ”₯0

Binary Tree Right Side View

MediumΒ·LeetCode #199 Pro OnlyBasic Reinforcement
LeetCode
01

Problem Statement

You are given the root of a binary tree.

Imagine you stand on the right side of the tree and look at it. Return the values of the nodes you can see, ordered from the top level down to the bottom level.

A node is visible when nothing on its level sits to its right. A node can be visible even if it is a left child.

Input

The root of a binary tree. The tree may be empty.

Output

An array of the visible node values, from top to bottom.

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

Level 0 shows 1. Level 1 shows 3, its right child. Level 2 has 5 (under the left child 2) and 4 (under the right child 3). 4 is further right, so it hides 5.

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

Nodes 4 and 5 are both in the left subtree. Nothing is to their right on their levels, so they are visible.

Example 3
Input: root = []
Output: []

An empty tree has nothing to see.

Constraints
  • β€’ The number of nodes is between 0 and 100.
  • β€’ βˆ’100 ≀ Node.val ≀ 100
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

β€œlook at the tree from the right side”
β†’ Only one node per level is visible, so the answer has one value per depth.
β€œvalues from top to bottom”
β†’ The answer is in level order, so a level-by-level walk produces it directly.
β€œnodes you can see”
β†’ A node hidden by a node further right on its level is not counted. Only the rightmost node of each level matters.
03

Approach Complexity Ladder

Brute Force β€” keep every level, then read the last oneO(N)O(N)

Store each full level in a list, then take the back of each list

Better β€” BFS, keep the last node of each roundO(N)O(W)

Level-order traversal; the last node popped in each round goes into the answer

Optimal β€” DFS, right child firstO(N)O(H)

Visit the right subtree before the left; the first node reached at each depth is the answer for that depth

Brute Force Intuition

Run a normal level-order traversal, but save every level as a whole list. When the traversal ends, the answer is the last value of each list.

Recognition rule for this sub-pattern:

Compute something per level: last node (right view), max, average, width.
Remember this Β· Tree BFS
Trigger
Level order, per-level max/average, right side view, minimum depth, nodes at distance k.
Think
Does the question care about depth / rows rather than paths?
Key question
How many nodes are in the current level (queue size at the start of the level)?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Tree BFS translate into production architectures and low-level memory mechanics.