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.
This approach involves using a queue to perform a level order traversal of the tree. At each level of the tree, we'll connect the 'next' pointers of all nodes at that level. Since it is a perfect binary tree, each node at a level is directly followed by its sibling or by NULL if it is the last node at that level.
This C code uses a pointer 'current' to iterate levels starting from the root. Another pointer 'head' iterates nodes across a level, connecting left and right children. Each right child's next is connected to the next node's left child, if it exists.
C++
Java
Python
C#
JavaScript
Time Complexity: O(N), where N is the number of nodes, since each node is processed once.
Space Complexity: O(1), as no extra space other than pointers is used—the perfect use of a constant space solution.
The recursive method explores the tree depth-first and connects nodes level-wise. This approach effectively utilizes the call stack for traversal, aligning with the tree's inherent properties.
This C function uses recursion to traverse the tree, performing connections between each node's immediate left and right children, and recursively developing connections within each subtree.
C++
Java
Python
C#
JavaScript
Time Complexity: O(N), which implies visiting each node once.
Space Complexity: O(logN) for recursion stack, since depth is proportional to the logarithm of node count.
| Approach | Complexity |
|---|---|
| Level Order Traversal (Breadth-First Search) | Time Complexity: O(N), where N is the number of nodes, since each node is processed once. |
| Recursive Method | Time Complexity: O(N), which implies visiting each node once. |
| 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 |
Populating Next Right Pointers in Each Node - Leetcode 116 - Python • NeetCode • 49,975 views views
Watch 9 more video solutions →Practice Populating Next Right Pointers in Each Node with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor