Trees · Binary Tree Views

Binary Tree Level Linking

Connecting every node to its next right neighbor on the same level using the previous level's already-established links as a substitute for a queue, walking level by level with O(1) extra space instead of O(w) for a BFS queue.

  • 7 min read
  • 9 practice questions
  • 2 graded problems
On this page
  1. Description
  2. The approach
  3. A second flavor: a perfect tree needs no dummy
  4. Where you'll see it
  5. Which tool for which job?
  6. Common mistakes
  7. Check yourself
  8. Key takeaways
  9. Quick revise
  10. Practice

Description

Level linking fills in each node's next pointer so it points at the node to its right on the same level, or null at the level's end. A BFS queue does it directly, but holds a whole level at once. The O(1)-space trick builds each level's links while walking the level above it, which is already linked, using a dummy node to collect the next level's chain.

We'll go from simple to more technical:

  1. The pattern: walk a linked level, chain the level below behind a dummy.
  2. A second flavor: the shortcut for perfect trees.
  3. Where sibling pointers pay off (database indexes).
  4. The shape assumptions that break, and when a queue is simply fine.

The approach

With a queue you'd take one level at a time and link its nodes left to right. But the queue holds a whole level, about n/2 nodes in a full tree. Here's the O(1)-space idea. Once a level is linked, its next pointers already form a path across it. So walk that path, and as you pass each node, attach its children to a chain for the level below. A dummy node starts the chain, so the first child needs no special case. When the walk ends, dummy.next is the first node of the level below.

right

right

8

3

6

1

9

right

right

8

3

6

1

9

Walking the levelChildren chained behind the dummyThe next level starts at
83, then 63
3 -> 61 (from 3), then 9 (from 6)1
1 -> 9nonestop

The last level is linked 1 -> 9, across two different parents. Note that 3 has no left child, so "the next level starts at levelStart.left" would have been wrong.

Java

Node connect(Node root) {
    Node levelStart = root;
    while (levelStart != null) {
        Node dummy = new Node(0);   // placeholder - dummy.next ends up as the next level's first node
        Node tail = dummy;
        Node cur = levelStart;
        while (cur != null) {                          // walk the current level via its own next pointers
            if (cur.left != null) { tail.next = cur.left; tail = tail.next; }
            if (cur.right != null) { tail.next = cur.right; tail = tail.next; }
            cur = cur.next;
        }
        levelStart = dummy.next;                        // move down to the level just linked
    }
    return root;
}

Python

def connect(root: Node | None) -> Node | None:
    level_start = root
    while level_start is not None:
        dummy = Node(0)      # placeholder - dummy.next ends up as the next level's first node
        tail = dummy
        cur = level_start
        while cur is not None:                     # walk the current level via its own next pointers
            if cur.left is not None:
                tail.next = cur.left
                tail = tail.next
            if cur.right is not None:
                tail.next = cur.right
                tail = tail.next
            cur = cur.next
        level_start = dummy.next                   # move down to the level just linked
    return root

The dummy is the same trick as in linked-list merging: tail always has somewhere to attach.

A second flavor: a perfect tree needs no dummy

In a perfect tree, every internal node has two children and every leaf is at the same depth. Then each level is fully predictable, and two links per node do the whole job:

Java

Node connect(Node root) {
    Node levelStart = root;
    while (levelStart != null && levelStart.left != null) {
        Node cur = levelStart;
        while (cur != null) {
            cur.left.next = cur.right;                             // siblings under the same parent
            if (cur.next != null) cur.right.next = cur.next.left;  // across the parent boundary
            cur = cur.next;
        }
        levelStart = levelStart.left;                              // next level starts at the far left
    }
    return root;
}

Python

def connect(root: Node | None) -> Node | None:
    level_start = root
    while level_start is not None and level_start.left is not None:
        cur = level_start
        while cur is not None:
            cur.left.next = cur.right                      # siblings under the same parent
            if cur.next is not None:
                cur.right.next = cur.next.left             # across the parent boundary
            cur = cur.next
        level_start = level_start.left                     # next level starts at the far left
    return root

The first link joins two siblings. The second crosses from one parent's right child to the next parent's left child, found through the parent's own next. Both assume every node has two children, which is why this only works on perfect trees.

Where you'll see it

B+-trees, the index structure behind most relational databases, link the pages on their bottom level with sibling pointers. A range scan walks down to its first key, then moves sideways from page to page instead of climbing back up the tree.

Which tool for which job?

You needUse
next pointers on any binary tree, O(1) spacewalk each linked level, chain children behind a dummy
The same on a perfect treeleft.next = right; right.next = next.left
Simplest code, memory no concernBFS with a queue, linking within each level
Level-by-level work after linkingwalk the next chains - no queue needed
Range scans across a levelsibling pointers (as in B+-tree leaves)

Common mistakes

MistakeWhat happensDo this instead
The perfect-tree rule on an imperfect treeNullPointerException on a missing childthe dummy-node walk
Linking only siblingschains stop at parent boundariesalso link right.next = next.left
Starting the next level at level.leftmisses levels whose first node has no left childthe dummy's next is the next level's start
Writing a level while still reading itlinks cross levelsread level k, write level k + 1
Forgetting the dummy resets per levellevels chain into each othera new dummy for each level

Check yourself

Try to answer before opening each one - recalling beats re-reading.

1. In the general version, why can't the next level simply start at levelStart.left?

Show answer

levelStart may have no left child, or no children at all, while nodes further along the level do. In the example, 3 has only a right child. The next level starts with the first child of any node on the level, and the dummy catches it: dummy.next is whichever child was attached first.

2. The perfect-tree version sets only cur.left.next = cur.right. In [1, 2, 3, 4, 5, 6, 7], what does walking the bottom level through next give?

Show answer

4 5, and then nothing. Siblings are linked, 4 to 5 and 6 to 7, but 5 never links to 6, because that link crosses from parent 2 to parent 3. cur.right.next = cur.next.left adds it.

3. Why is it safe to walk level k through its next pointers while writing level k + 1's?

Show answer

The loop only reads level k's pointers, which were all set in the previous round. It only writes level k + 1's, which nothing reads until the next round. Reading and writing never touch the same level at once.

Key takeaways

  • A linked level's next pointers are a path across it. Walking that path replaces the queue, for O(1) extra space.
  • In a general tree, a dummy node catches the next level's first node, whoever its parent is.
  • A perfect tree needs two links per node: siblings, and across the parent boundary.
  • The perfect-tree rule crashes on a missing child. Use it only on trees guaranteed to be perfect.
  • The loop reads one level and writes the next, never the same one.

Quick revise

The whole skill on one page, for the night before an interview.

Everything from the lesson and the deep dive on one page. Skim it before practice or an interview, and open the full lesson for anything that doesn't ring a bell.

Pick the tool

You needUse
Any tree, O(1) spacedummy-node walk
Perfect treesibling + cross links
SimplestBFS queue
Level work afterwardsfollow next

Snippets worth knowing by heart

Java

Node level = root;
while (level != null) {
    Node dummy = new Node(0), tail = dummy;
    for (Node cur = level; cur != null; cur = cur.next) {       // read level k
        if (cur.left != null) { tail.next = cur.left; tail = tail.next; }    // write level k + 1
        if (cur.right != null) { tail.next = cur.right; tail = tail.next; }
    }
    level = dummy.next;                                         // the next level's first node
}

Python

start = root
while start and start.left:                  # perfect trees only
    cur = start
    while cur:
        cur.left.next = cur.right
        if cur.next:
            cur.right.next = cur.next.left   # across the parent boundary
        cur = cur.next
    start = start.left

The traps

  • The perfect rule on imperfect trees: a missing child throws a NullPointerException. Use the dummy-node walk.
  • Only sibling links: chains stop at parent boundaries. Also link right.next to next.left.
  • level.left as the next start: levels whose first node has no left child are skipped. Start at the dummy's next.
  • Reading and writing the same level: links cross levels. Read level k, write level k + 1.
  • A dummy shared across levels: levels chain into each other. Make a new dummy for each level.

Staff-level points, one line each

  • Invariant: levels up to k are linked before round k.
  • 500 random trees: dummy walk = BFS links.
  • Complete tree of 100,000: BFS queue peaked at 34,465; the walk needs a few pointers.
  • Perfect rule on an imperfect tree: NullPointerException.
  • B+-tree leaves use sibling links for range scans.

Say it in an interview

  • "I walk each level along the next pointers I already set, and chain the level below behind a dummy."
  • "That's O(n) time and O(1) extra space."
  • "The dummy means the next level's first node needs no special case."
  • "For a perfect tree there's a shorter rule, but it breaks on gaps."

Practice

This skill comes with 9 multiple-choice questions that tell you which misconception a wrong answer reveals and 2 graded problems. Your answers are graded and your mastery is tracked, so you'll know when the skill holds up. Running code and saving progress need a free account.

Practice Binary Tree Level Linking free

Populating Next Right Pointers in Each Node

MediumProCoding problemJava · Python

Every node of this binary tree has an extra next pointer, which starts out null. Point each node's next at the node to its right on the same level, and leave the rightmost node of each level pointing at null. The tree can have any shape.

The full statement, the examples and the hidden test suite are part of AI Proof Engineers Pro.

Next Right Pointers in a Perfect Binary Tree

MediumProCoding problemJava · Python

You're given a perfect binary tree - every internal node has exactly two children and all leaves are on the same level - where each node has an extra next pointer (initially null).

The full statement, the examples and the hidden test suite are part of AI Proof Engineers Pro.

Practice Binary Tree Level Linking free

No card needed. Lessons, practice questions and easy problems are free forever.

Free plan, no card · 7-day Pro trial · Cancel anytime