Populating Next Right Pointers
Populating Next Right Pointers in Each Node is LeetCode 116 (Medium). You get a perfect binary tree: every parent has two children and all leaves sit on the same level. Each node also has a pointer next, which starts as null.
- Point every node's
nextat the node to its right on the same level. - The last node on each level keeps
next = null. - Return the root.
The follow-up asks for constant extra space. The recursion stack is allowed and does not count.
- The number of nodes in the tree is in the range [0, 2¹² - 1].
- -1000 <= Node.val <= 1000
Intuition
Once a level is linked, it is a linked list you can walk with next. That walk can replace the BFS queue for the level below. Stand on each node of the linked level and wire its two children:
- Same parent:
node.left.next = node.right. - Different parents:
node.right.next = node.next.left, the left child of the node to the right.
The perfect shape is what makes the second link safe: if node.next exists, it sits on the same level as node, so it has a left child too. The root is a finished level of one, so start there and wire each level from the one above.
To connect nodes at the same level is a level-order job. When the links you are building are themselves a way to walk a level, the queue can go: each finished level carries you across while you wire the next.
Approach
Before reading on, wire the tree level by level with a queue. Then look at the level you just finished: its next pointers already visit it left to right. Use that walk to wire the next level and drop the queue. Aim for O(1) extra space.
Two ways to solve it
Use the next pointers of the finished level to wire the level below.
- No queue: two pointers,
leftmostandhead. - Needs a perfect tree: it reads
head.next.leftwithout checking it.
The answer to the constant-space follow-up.
Pop one level at a time and link each node to the one popped after it.
- Obvious first idea: plain level-order traversal.
- Works on any tree, perfect or not.
- Cost: the queue holds a whole level, up to 2,048 nodes here.
Fine when extra space is allowed.
Both visit each node once; the level walk wins because it needs no queue at all. The steps, code and live demo follow the level walk, and the BFS code comes after the demo.
Keep a pointer to the start of each level
Set leftmost = root. The first node of every level is the left child of the first node above it, so this one pointer is enough to find where each new level begins.
Walk the level through its next pointers
Set head = leftmost and move with head = head.next until it is null. The previous round linked this level, so the walk visits every node on it from left to right without any queue.
Wire the two children of each node
For each head, link its left child to its right child. Then, if head.next is not null, link its right child to head.next.left. The first link joins siblings; the second joins the children of two neighbouring parents.
Drop down a level and stop at the leaves
After the walk, set leftmost = leftmost.left. Stop when leftmost has no left child: that is the leaf level, which was already linked from above and has no children of its own to wire.
Code in Python, C++ and Java
next starts as null. The root's level has one node, so it is already a finished linked list: 1 → null. That level will be the rails for wiring the level below it.next exactly like the one above.next exactly like the one above.next: 1 → null; 2 → 3 → null; 4 → 5 → 6 → 7 → null. Only leftmost and head were stored, never a queue.next starts as null. The root's level has one node, so it is already a finished linked list: 1 → null. That level will be the rails for wiring the level below it.next exactly like the one above.next exactly like the one above.next exactly like the one above.next: 1 → null; 2 → 3 → null; 4 → 5 → 6 → 7 → null; 8 → 9 → 10 → 11 → 12 → 13 → 14 → 15 → null. Only leftmost and head were stored, never a queue.next starts as null. The root's level has one node, so it is already a finished linked list: 1 → null. There is no level below it.next: 1 → null. Only leftmost and head were stored, never a queue.Populating next right pointers with BFS
The queue always holds exactly one level when the inner loop starts. Each popped node becomes the next of the node popped just before it, and prev resets to null at the start of every level so no link crosses from one level to the next.
Common pitfalls
Linking only siblings
head.left.next = head.right
head.left.next = head.right
if head.next:
head.right.next = head.next.leftOn [1,2,3,4,5,6,7], node 5 has no sibling to its right, so it keeps null instead of pointing to 6. The level falls apart into pairs, and on a deeper tree the next walk stops at 5 and never wires 6's children.
Looping while leftmost is not null
while leftmost:
while leftmost and leftmost.left:
The loop then reaches the leaf level and runs head.left.next on a node with no children, which crashes. A single-node tree [1] fails on the very first step.
Edge cases
[]leftmost is null, so the loop never runs and null is returned.
[1]The root has no left child, so the loop never runs. The root's next was already null, which is correct.
Complexity
head once and makes at most two pointer writes. Only leftmost and head are stored, so the extra space stays constant no matter how deep the tree is.Populating Next Right Pointers FAQ
What changes in Populating Next Right Pointers II?
LeetCode 117 drops the perfect-tree promise, so head.next.left may not exist and a child may be missing. Keep a dummy node at the start of the next level and a tail pointer; while walking the current level, append each child that exists to tail. It is still O(n) time and O(1) space, and it also solves this problem.
Is the recursive solution also O(1) space?
The problem says the recursion stack does not count, so yes for grading. A recursive version makes the same two links at each node and then recurses into both children. The stack still holds one frame per level, at most 12 here, so it is O(log n) in real memory.