Watch 10 video solutions for Populating Next Right Pointers in Each Node, a medium level problem involving Linked List, Tree, Depth-First Search. This walkthrough by NeetCode has 49,975 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
Initially, all next pointers are set to NULL.
Example 1:
Input: root = [1,2,3,4,5,6,7] Output: [1,#,2,3,#,4,5,6,7,#] Explanation: Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
Example 2:
Input: root = [] Output: []
Constraints:
[0, 212 - 1].-1000 <= Node.val <= 1000
Follow-up:
Problem Overview: Each node in a perfect binary tree has a next pointer that should point to its immediate neighbor on the right at the same level. If there is no neighbor, the pointer must be null. The task is to populate these pointers efficiently while traversing the tree.
Approach 1: Level Order Traversal (Breadth-First Search) (Time: O(n), Space: O(n))
This approach performs a level-by-level traversal using a queue, a standard pattern in breadth-first search. Push the root node into the queue, then process nodes one level at a time. For each level, iterate through the queue size and connect the current node’s next pointer to the next node in the queue unless it is the last node in that level. The queue naturally groups nodes by depth, making pointer connections straightforward. This method works for any binary tree structure and is easy to reason about during interviews.
Approach 2: Recursive Method (Time: O(n), Space: O(h))
This method leverages the properties of a perfect binary tree. Each node’s left child should point to its right child, and each node’s right child should point to the left child of the node’s next neighbor. Use depth-first search recursion to propagate these relationships down the tree. At every node, connect node.left.next = node.right. If the current node has a next, connect node.right.next = node.next.left. Recursively process the left and right subtrees. The recursion depth equals the tree height, which is log n for a perfect tree.
Recommended for interviews: Start with the BFS level order approach because it clearly demonstrates understanding of tree traversal and pointer linking. Interviewers often expect candidates to recognize that the tree is perfect and optimize further using the recursive constant-extra-space method. Showing both approaches demonstrates strong grasp of tree traversal patterns and pointer manipulation.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Level Order Traversal (BFS) | O(n) | O(n) | General solution for binary trees when using a queue is acceptable |
| Recursive Pointer Linking | O(n) | O(h) | Optimal for perfect binary trees when minimizing extra memory |