Populating Next Right Pointers in Each Node - Problem
Connect the Dots: Building Horizontal Bridges in a Perfect Binary Tree

Imagine you have a perfect binary tree where every level is completely filled and all leaves are at the same depth. Your task is to connect each node to its immediate right neighbor on the same level.

Each node has a special next pointer that should point to the node immediately to its right on the same level. If there's no right neighbor (like the rightmost node on each level), the next pointer should be NULL.

Node Structure:
struct Node {
  int val;
  Node *left;
  Node *right;
  Node *next; // This is what we need to populate!
}


Goal: Transform a regular perfect binary tree into a horizontally connected tree where you can traverse each level from left to right using the next pointers.

Challenge: Can you solve this without using extra space for a queue or additional data structures?

Input & Output

perfect_tree.py — Complete Perfect Binary Tree
$ Input: root = [1,2,3,4,5,6,7]
Output: [1,#,2,3,#,4,5,6,7,#]
💡 Note: The tree has 3 levels. After connecting, level 0 has node 1, level 1 has 2->3, and level 2 has 4->5->6->7. The # represents NULL next pointers.
single_node.py — Single Root Node
$ Input: root = [1]
Output: [1,#]
💡 Note: A single node tree has no siblings to connect to, so its next pointer remains NULL.
empty_tree.py — Empty Tree Edge Case
$ Input: root = []
Output: []
💡 Note: An empty tree returns as-is with no modifications needed.

Constraints

  • The number of nodes in the tree is in the range [0, 212 - 1]
  • -1000 ≤ Node.val ≤ 1000
  • The tree is guaranteed to be a perfect binary tree
  • All leaves are on the same level and every parent has two children

Visualization

Tap to expand
Populating Next Right Pointers in Each Node INPUT Perfect Binary Tree 1 2 3 4 5 6 7 struct Node { int val; Node *left, *right; Node *next; // populate! Input: [1,2,3,4,5,6,7] ALGORITHM STEPS 1 Start at Root Use leftmost node of each level 2 Connect Children node.left.next = node.right 3 Cross-Subtree Connect node.right.next = node.next.left 4 Move to Next Level Go to leftmost (curr.left) Level-by-Level Processing 2 3 next NULL O(1) space - use next pointers! Time: O(n) | Space: O(1) curr = root while curr.left: connect_level(curr) FINAL RESULT Horizontally Connected Tree 1 # 2 3 # 4 5 6 7 # Legend next pointer # = NULL Output: [1,#,2,3,#,4,5,6,7,#] OK - All Connected! Key Insight: Use already-established next pointers to traverse each level! Process level N to connect level N+1. The leftmost node of each level acts as a "head" for traversal. No queue needed - O(1) space! Two connections per node: (1) left.next = right, (2) right.next = next.left (cross-subtree bridge) TutorialsPoint - Populating Next Right Pointers in Each Node | Optimal Solution
Asked in
Microsoft 45 Amazon 38 Facebook 32 Google 28
124.6K Views
High Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen