MediumLinked ListTreeDepth-First SearchBreadth-First SearchBinary TreeUpdated Sep 2026

Populating Next Right Pointers in Each Node

Asked at Meta, Microsoft, Amazon, Google and 1 more

Difficulty

Medium

Time

O(n)

Space

O(1)

Pattern

Linked List

Companies

5

Problem

You are given the root of a perfect binary tree. A perfect tree means every level is completely full: every internal node has exactly two children, and all leaves sit on the same level.

Each node has the usual left and right pointers, plus an extra next pointer. At the start, every next pointer is NULL. Your job is to fill in those next pointers so that each node points to the node immediately to its right on the same level. The rightmost node on each level keeps next = NULL.

Return the root of the tree after the pointers are set. The tree structure itself does not change — you only add the next links. The follow-up asks you to do this with O(1) extra space, so a queue-based level-order traversal is not allowed.

Connect each node to its right neighbor

4next → 52next → 35next → 61next = NULL6next → 73next = NULL7next = NULLLevel 1: 2 → 3. Level 2: 4 → 5 → 6 → 7. The rightmost node on eachlevel stays NULL.
Every node on a level should point to the node just to its right; the last node on each level points to NULL.

Constraints

  • ●The number of nodes is in the range [0, 2^12 - 1]
  • ●-1000 <= Node.val <= 1000
  • ●The tree is guaranteed to be a perfect binary tree (all leaves on the same level, every parent has two children)

Examples

Example 1
Inputroot = [1,2,3,4,5,6,7]
Output[1,#,2,3,#,4,5,6,7,#]
WhyLevel 0 has only node 1, so its next stays NULL. On level 1, node 2 points to node 3, and node 3 points to NULL. On level 2, the chain is 4 → 5 → 6 → 7 → NULL. The output lists each level followed by '#' to mark the end of that level.
Example 2
Inputroot = []
Output[]
WhyThe tree is empty, so there are no nodes and nothing to connect. The result is an empty list.

Asked At

5 companies

How to Think About It

1

BFS with a queue: simple level-order traversal. Connect each node to the next node in the queue. O(n) time, O(n) space — but the problem asks for O(1) space.

2

Using next pointers: start at the root. For each level, use the next pointers to traverse left-to-right. Connect children: left child's next = right child. Right child's next = parent's next's left child (if parent's next exists).

3

Recursive approach: connect children during recursion. If node.left exists, node.left.next = node.right. If node.right exists and node.next exists, node.right.next = node.next.left. Recurse on children.

4

Why it works: by the time you process a node, its next pointer already gives you access to the next level. You use this to "jump across" the tree without a queue.

5

Edge cases: single node (no connections needed), root is null, the problem guarantees a perfect binary tree (every level is fully filled).

Optimal Approach

Start at root. For each level (while current node is not null):

  1. Connect left child: node.left.next = node.right.
  2. Connect right child: if node.next exists, node.right.next = node.next.left.
  3. Move to next node in current level via node.next.
  4. When current level is done, move to next level via root = root.left.

Repeat until root is null (no more levels).

Time: O(n) — visit each node once. Space: O(1) — only pointer variables.

Use next pointers to walk each level

42head51leftmost = root63head.next7Step 1 — start at the leftmost node of the level and walk right usingnext.44.next = 5255.next = 6166.next = 7377.next = NULLStep 2 — for each head, set head.left.next = head.right, thenhead.right.next = head.next.left.left.next = rightright.next = parent.next.leftthen head = head.nextwhen level ends: leftmost = leftmost.left
For each level, walk left to right using the next pointers you already built, and wire up the children below.
ComplexityTime O(n)Space O(1)Every node is visited once, and the traversal uses only a few pointer variables instead of a queue.

What Trips People Up in Real Interviews

1

Using BFS with a queue — this works but uses O(n) space. The interviewer wants O(1) space, so you must use the next pointers.

2

Forgetting to handle the right-to-left connection. When connecting right children, you need to look across to the next subtree via the parent's next pointer.

3

Not using level-by-level processing. You cannot connect nodes across levels — process each level completely before moving to the next.

4

Getting the recursion order wrong. Process left and right children at the same level, then recurse. If you recurse too early, next pointers may not be set yet.

Solution Code

O(n) time
def connect(root):
    if not root:
        return root
    leftmost = root
    while leftmost.left:
        head = leftmost
        while head:
            head.left.next = head.right
            if head.next:
                head.right.next = head.next.left
            head = head.next
        leftmost = leftmost.left
    return root

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Populating Next Right Pointers in Each Node problem?

Populating Next Right Pointers in Each Node asks you to connect each node to its next right neighbor in a perfect binary tree. You must use `O(1)` extra space (no queue), which rules out BFS and forces you to use the already-established `next` pointers to traverse.

How do you solve Populating Next Right Pointers in Each Node?

The optimal approach runs in O(n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Populating Next Right Pointers in Each Node?

Populating Next Right Pointers in Each Node is asked at Meta, Microsoft, Amazon, Google, Bloomberg. It is a medium difficulty problem.

What are common mistakes on Populating Next Right Pointers in Each Node?
  • Using BFS with a queue — this works but uses `O(n)` space. The interviewer wants `O(1)` space, so you must use the `next` pointers.
  • Forgetting to handle the right-to-left connection. When connecting right children, you need to look across to the next subtree via the parent's `next` pointer.
  • Not using level-by-level processing. You cannot connect nodes across levels — process each level completely before moving to the next.
  • Getting the recursion order wrong. Process left and right children at the same level, then recurse. If you recurse too early, `next` pointers may not be set yet.