2Brute2Best
🔥0

Binary Tree Level Order Traversal II

Medium·LeetCode #107 Pro OnlyBasic Reinforcement
LeetCode
01

Problem Statement

You are given the root of a binary tree. Return the node values one row at a time, like a normal level order traversal, but from the bottom row up: the deepest row comes first, then the row above it, and the root's row comes last.

Inside each row, keep the usual order: left to right. Only the order of the rows is flipped.

Input

The root of a binary tree. It can be empty (null).

In the examples, a tree is written in level order: the root first, then each row left to right. A null marks a missing child.

Output

A list of rows, with the deepest row first and the root's row last. Each row is read left to right.

Example 1
Input: root = [3, 9, 20, null, null, 15, 7]
Output: [[15, 7], [9, 20], [3]]

The top-down rows are [3], [9, 20] and [15, 7]. Reading them from the bottom up gives [15, 7], [9, 20] and [3]. Inside [15, 7] the order is still left to right.

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

A single node is one row.

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

No nodes, so no rows.

Constraints
  • • The number of nodes in the tree is in the range [0, 2000].
  • • −1000 ≤ Node.val ≤ 1000
02

Pattern Recognition Clues

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

“bottom-up level order”
→ The same rows as LC 102, only in the opposite order.
“from leaf to root”
→ The deepest row must come first in the answer.
“left to right within each level”
→ Inside a row, the order does not change, so the rows are flipped but the values are not.
03

Approach Complexity Ladder

Brute Force — walk the tree from the deepest row upO(N · H)O(H)

Find the height, then for depth d from H − 1 down to 0, walk the whole tree and keep depth d

Optimal — level order, then reverse the list of rowsO(N)O(N)

Run the normal BFS unchanged, then reverse the outer list once

Brute Force Intuition

Find the height H. Then, for depth d from H − 1 down to 0, walk the whole tree and collect the nodes at depth d. The rows come out bottom-up directly, so nothing needs reversing.

Recognition rule for this sub-pattern:

Pop exactly `size` nodes per round; that round is one level.
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.