2Brute2Best
πŸ”₯0

Populating Next Right Pointers in Each Node II

MediumΒ·LeetCode #117 Pro OnlyMedium
LeetCode
01

Problem Statement

You are given a binary tree. It is not necessarily perfect: a node can have two children, one child, or none. Each node has a next pointer, and all of them start as null.

Set every next pointer to the node on its right, on the same level. If there is no node on the right, set it to null. Return the root.

Input

The root of a binary tree (it can be empty). Each node has val, left, right and next.

Output

The same root, with every next pointer filled in.

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

The output lists the nodes level by level, following the next pointers, and # marks the end of each level. Node 5 points to node 7, even though their parents (2 and 3) are different. Node 3 has no left child, so the level below does not start under it.

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

An empty tree has no nodes, so there is nothing to set.

Constraints
  • β€’ The number of nodes is in the range [0, 6000]
  • β€’ βˆ’100 ≀ Node.val ≀ 100
  • β€’ Follow-up: can you use only constant extra space?
02

Pattern Recognition Clues

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

β€œconnect each node to the next node on the same level”
β†’ A level is a list. The level above supplies that list through next pointers, or a queue supplies it.
β€œnot necessarily perfect”
β†’ Some nodes have one child or none, so the left-child trick of LC 116 fails. The level below does not always start at leftmost->left.
β€œconstant extra space follow-up”
β†’ No queue. Build the next level behind a dummy node while walking the current one.
03

Approach Complexity Ladder

Brute Force β€” level order queue, link each roundO(N)O(W)

BFS with a queue; link each popped node to the front of the queue unless it is the last of its round

Optimal β€” build the level below behind a dummy nodeO(N)O(1)

Walk the level with next and append each node's children to the level below, using a dummy head and a tail

Brute Force Intuition

The same idea as LC 116: go level by level with a queue. Each popped node points to the next node popped in the same round. The last node of each round keeps next = null. Gaps do not matter, because the queue only holds the nodes that exist.

Recognition rule for this sub-pattern:

Connect each node to the next node on the same level, using the level above as a linked list.
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.