Skip to main content
Medium Trees / DFS Medium frequency

Populating Next Right Pointers in Each Node

Open on LeetCode

Approach Summary

Level-order using existing next pointers. For each node, link node.left.next = node.right and node.right.next = node.next?.left.

How to Recognize This Pattern

  • Perfect binary tree — use next pointers of current level to traverse
  • O(1) space: use the level you just connected to build the next

Complexity Analysis

Time Complexity

O(n)

Space Complexity

O(1)

Tags

Linked List Tree DFS BFS Binary Tree

This site is free. If these guides are helping your prep, consider buying me a coffee. ☕

Support →
Buy me a coffee