Populating Next Right Pointers in Each Node
Use the next pointers of the row above as the queue. Start at the root. For each row, walk it with next and link the children of each node. Between two neighbouring parents, add one cross link from the right child to the next parent's left child. Then move down to the leftmost node of the row below.
Children of neighbouring nodes are neighbours too, so walking one row with next links the whole row below it.
Problem
You are given a perfect binary tree: every parent has exactly two children, and all leaves are on the same level. 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.
The root of a perfect binary tree (it can be empty). Each node has val, left, right and next.
The same root, with every next pointer filled in.
The output lists the nodes level by level, following the next pointers. # marks the end of each level. Node 5 points to node 6, even though their parents (2 and 3) are different.
An empty tree has no nodes, so there is nothing to set.
- β’ The number of nodes is in the range [0, 2^12 β 1]
- β’ β1000 β€ Node.val β€ 1000
- β’ Follow-up: can you use only constant extra space?
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck β each hint is counted.
- 1.
What is a
nextpointer here? Which nodes are on the same level? - 2.
Can a node's next be a node with a different parent? Find one example in the tree above.
- 3.
The tree is perfect. What does that tell me about how many nodes each level has?
- 4.
A queue of one level gives me the nodes in order. How much extra space does it take, and can I avoid it?
- 5.
Once a level is linked by next, can I use those links to walk that level and link the one below?
Brute force approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | BFS with a queue; link each popped node to the front of the queue unless it is the last of its round | O(N) | O(W) |
| Optimal | Start at the leftmost node; for each node on a row, link its children, plus one cross link to the next parent's left child | O(N) | O(1) |
Why does this belong to this pattern?
- βconnect each node to its right neighbourβEach row is a list of nodes. The row above can supply that list through its next pointers.
- βperfect binary treeβEvery row is full, so each node above the leaves has exactly two children. That lets the walk use cur->left without a null check.
- βconstant extra space follow-upβNo queue. The next pointers we are setting become the queue.
This is level order with the queue replaced by the next pointers. A queue for one row is just the row's nodes in order. Once the row above is linked by next, that row is the list, so no queue is needed. The new part is the cross-parent link (right child β next parent's left child). Populating Next Right Pointers II (LC 117) uses the same walk, but its tree has gaps, so the row below cannot be found from the leftmost node alone.
Don't make this mistake
Node 5 (child of 2) and node 6 (child of 3) have different parents, so the first line never links them. Without the second line, 5's next stays null. The output becomes [1, #, 2, 3, #, 4, 5, #, 6, 7, #], and 5 should point to 6.
When a round ends, the front of the queue is the first node of the next row, not a neighbour. On [1, 2, 3, 4, 5, 6, 7], node 3 is the last node of row 1, but the queue already holds 4, so 3 would point to 4. It should be null.
On the bottom row the leaves have no children, so cur->left is null. The line cur->left->next = cur->right then dereferences a null pointer and crashes. The loop must stop when the row below does not exist.
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Tree BFS translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.