2Brute2Best
πŸ”₯0

Find Largest Value in Each Tree Row

MediumΒ·LeetCode #515 Pro OnlySlight Variation
LeetCode
01

Problem Statement

You are given the root of a binary tree.

A row is all the nodes at the same depth. Row 0 is the root's row, row 1 is the row below it, and so on.

For each row, find the largest node value in that row. Return one number per row, from the top row down.

Input

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

Output

An array with one number per row: the largest value in that row, top to bottom.

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

Row 0 has only 1. Row 1 has 3 and 2, so the largest is 3. Row 2 has 5, 3 and 9, so the largest is 9.

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

Row 0 has 1. Row 1 has 2 and 3, so the largest is 3.

Constraints
  • β€’ The number of nodes is between 0 and 10^4.
  • β€’ βˆ’2^31 ≀ Node.val ≀ 2^31 βˆ’ 1
02

Pattern Recognition Clues

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

β€œlargest value in each row”
β†’ One maximum per row. It is a per-level aggregate, like the averages question, with max in place of sum.
β€œrow (0-indexed)”
β†’ A row is a depth, so the answer is indexed by depth and each row gets exactly one value.
β€œreturn an array”
β†’ The answer is ordered top to bottom, which is the order in which BFS finishes the rows.
03

Approach Complexity Ladder

Brute Force β€” store each row, then take the maxO(N)O(N)

Store each full row in a list, then take the largest value in each list

Optimal β€” BFS, running max per roundO(N)O(W)

Level-order traversal; keep a running max for each round and write it out when the round ends

Brute Force Intuition

Run a level-order traversal and save each row as a complete list. When the traversal ends, the answer is the largest value in 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.