↓Skip to main content
  1. LeetCode/

LeetCode 117: Populating Next Right Pointers in Each Node II

·2 mins· ·
LeetCode Medium Linked-List Tree Depth-First-Search Breadth-First-Search Binary-Tree
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2022-11-18

  • Submissions: Wrong Answer → Runtime Error → Wrong Answer → Accepted

Approach
#

Each node’s next must point to the node on its right in the same level. Process the tree level by level with BFS (Tree Template 2): while handling a level, the next node in the queue is the one to the right, so every node except the last one in its level gets node.next = q[0].

My Attempts
#

In 2022 I wrote three recursive versions and none passed:

  • The idea was to wire the children while standing on root: the right child links to a child of root.next. But root.next may have no children, and then the search should continue to root.next.next; my code gave up. For [1,2,3,4,null,5,6,7,8,null,null,null,9], node 8 should skip childless 5 and link to 6’s child 9, but it got None.
  • One submission typed node instead of root, a Runtime Error.

On 2026-10-24 I switched to BFS and passed on the first try.

Solution
#

class Solution:
    def connect(self, root: 'Node') -> 'Node':
        if not root:
            return None
        q = deque([root])
        while q:
            n = len(q)
            for i in range(n):
                node = q.popleft()
                if i < n - 1:
                    node.next = q[0]
                if node.left:
                    q.append(node.left)
                if node.right:
                    q.append(node.right)
        return root

Follow-up: O(1) extra space
#

Once a level’s next pointers are set, the level is a linked list. Walk it and chain the next level’s children with a dummy head (Linked List Template 1):

class Solution:
    def connect(self, root: 'Node') -> 'Node':
        head = root
        while head:
            dummy = tail = Node(0)
            cur = head
            while cur:
                for child in (cur.left, cur.right):
                    if child:
                        tail.next = child
                        tail = child
                cur = cur.next
            head = dummy.next
        return root

Complexity
#

BFS: time O(n), space O(w) for the widest level. Linked-list version: time O(n), extra space O(1).

Mistakes to Avoid
#

  • In a non-perfect tree the neighbour may have no children; keep moving right until a node with children is found.
  • Do not link the last node of a level to the first node of the next level.

Interview Explanation
#

I process the tree level by level with BFS. Within a level the next node in the queue is the right neighbour, so every node but the last gets node.next = q[0]; that is O(n) time. For O(1) space, I walk the previous level through its next pointers as a linked list and chain the next level’s children with a dummy head.

Related

LeetCode 116: Populating Next Right Pointers in Each Node
·2 mins
LeetCode Medium Linked-List Tree Depth-First-Search Breadth-First-Search
LeetCode 116 note: in a perfect binary tree, walk each level through the next pointers already set on the level above: left.next = right and right.next = next.left, with O(1) extra space
LeetCode 1448: Count Good Nodes in Binary Tree
·1 min
LeetCode Medium Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 1448 note: pass the path maximum down during DFS; a node is good when its value is at least that maximum
LeetCode 199: Binary Tree Right Side View
·2 mins
LeetCode Medium Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 199 note: in a level-order traversal, each level’s rightmost node is what you see from the right
LeetCode 113: Path Sum II
·2 mins
LeetCode Medium Backtracking Tree Depth-First-Search Binary-Tree
LeetCode 113 note: backtrack with the current path and store a copy at a leaf whose total matches; values can be negative, so no early pruning
LeetCode 129: Sum Root to Leaf Numbers
·2 mins
LeetCode Medium Tree Depth-First-Search Binary-Tree
LeetCode 129 note: on the way down multiply the current number by 10 and add the node’s value, adding it to the total at a leaf
LeetCode 236: Lowest Common Ancestor of a Binary Tree
·2 mins
LeetCode Medium Tree Depth-First-Search Binary-Tree Binary-Lifting
LeetCode 236 note: recursion returns whichever of p or q a subtree contains; finding something on both sides means they split here, so this node is the LCA