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.

Visualization

Tap to expand
Perfect Binary Tree Next Pointer Connection123456789101112131415๐ŸŒŸ Magic: Each level's connections help build the next level!Green arrows show 'next' pointers connecting sibling nodes horizontally
Understanding the Visualization
1
Start at Top
Begin with the root node as our starting point
2
Connect Current Level
Use existing connections to traverse and connect children
3
Navigate Horizontally
Move right using next pointers we've already established
4
Descend to Next Level
Go down to the leftmost child and repeat the process
Key Takeaway
๐ŸŽฏ Key Insight: Transform the tree into a navigation system where each level's connections become the roadmap for efficiently connecting the next level, eliminating the need for extra space!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Each node is visited exactly once to set up connections

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

No additional space needed beyond a few pointers for navigation

n
2n
โœ“ Linear Space

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
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