AlgoMaster Logo

Populating Next Right Pointers in Each Node II

mediumFrequency6 min readUpdated June 23, 2026

Understanding the Problem

We have a binary tree where each node has an extra next pointer. Our job is to wire up every node's next to the node immediately to its right on the same level. If a node is the rightmost on its level, its next stays NULL.

The difference from LeetCode 116 (Populating Next Right Pointers in Each Node) is that this tree is not necessarily perfect. Nodes can be missing anywhere. In a perfect binary tree, the next right node is always predictable: a left child's next is its parent's right child, and so on. Here, a node and the node to its right can belong to different parents, with gaps in between. If the parent of a node has no children but the next parent does, the connection has to skip across to that further parent's children.

Simple parent-child relationships are not enough. The solution needs a way to traverse across an entire level regardless of which parent each node descends from.

Key Constraints:

  • 0 <= number of nodes <= 6000. The follow-up asks for O(1) extra space, which rules out BFS with a queue (that queue holds up to a full level, so O(n) space).
  • -100 <= Node.val <= 100. Values are irrelevant here; the algorithm only rewires pointers.
  • The empty tree ([]) is a valid input, so the code must handle a null root before touching any pointer.

Approach 1: BFS with Queue (Level-Order Traversal)

Intuition

To connect nodes on the same level, traverse the tree level by level. BFS does this directly. Process one level at a time: the nodes come off the queue in left-to-right order, so each node's next is the node that follows it within the same level. The last node on a level has no successor, so its next stays null.

This works for any binary tree shape, perfect or not, because BFS groups nodes by depth regardless of which parent they came from.

Algorithm

  1. If the root is null, return null immediately.
  2. Initialize a queue with the root node.
  3. While the queue is not empty, record the current level's size.
  4. Process each node in the level. For each node except the last, set its next to the node that follows it in the queue.
  5. Add each node's non-null children to the queue for the next level.
  6. Return the root.

Example Walkthrough

1Start BFS: enqueue root (1). levelSize=1
1current24537
1/8

Code

BFS stores an entire level of nodes in a queue, which is O(n) extra space for a wide tree. The follow-up asks for O(1) extra space. The next approach removes the queue by observing that once a level's next pointers are connected, that level is already a linked list that can be traversed top to bottom without any auxiliary storage.

Approach 2: Constant Space Using Previously Established Next Pointers

Intuition

Process the tree one level at a time, top to bottom. Once level k is fully connected, its nodes form a linked list through their next pointers. Walk across that list, and for each node, append its children to a separate linked list that becomes level k+1. The connected level serves as the iterator for building the level below it, so no queue is required.

Handling the gaps is the part that needs care. Because the tree is not perfect, a node may have only a left child, only a right child, or none. The children of consecutive nodes on level k might belong to different parents and still be adjacent on level k+1. The build step ignores parentage entirely: it appends whatever children exist, in the order they are encountered, and adjacency falls out automatically.

A dummy node makes the build step uniform. It serves as the fixed head of the next level's list, with a tail pointer tracking the last node appended so far. Every child found is attached after tail, whether it is the first child of the level or the tenth. After the whole level is processed, dummy.next points to the leftmost node of the next level, which becomes the new starting point.

Algorithm

  1. Start with current pointing to the root (level 0 is the root alone).
  2. While current is not null, begin a new level: create a fresh dummy node and set tail to it.
  3. Walk across the current level using next pointers. At each node, if it has a left child, attach it after tail and advance tail. Do the same for the right child.
  4. When the current level is fully traversed, dummy.next is the leftmost node of the next level (or null if the current level had no children).
  5. Set current to dummy.next and repeat from step 2.
  6. The outer loop ends when current is null, meaning the last processed level had no children.

Example Walkthrough

1Start: current = root (1). Process level 0 to wire up level 1
1current24537
1/9

Code