Populating Next Right Pointers in Each Node - codepath/compsci_guides GitHub Wiki
Unit 12 Session 2 Advanced (Click for link to problem statements)
Problem Highlights
- 💡 Difficulty: Medium
- ⏰ Time to complete: 25-30 mins
- 🛠️ Topics: Trees, Linked Lists, Level Order Traversal
1: U-nderstand
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
-
What is a perfect binary tree?
- A perfect binary tree is one where all internal nodes have exactly two children, and all leaves are at the same level.
-
What does the
next
pointer do?- It connects each node to its immediate right node at the same level. If no such node exists, the
next
pointer is set toNone
.
- It connects each node to its immediate right node at the same level. If no such node exists, the
HAPPY CASE
Input: [1, 2, 3, 4, 5, 6, 7]
Output: [(1, None), (2, 3), (3, None), (4, 5), (5, 6), (6, 7), (7, None)]
Explanation: All nodes are connected to their adjacent right nodes.
Input: [1, 2, 3]
Output: [(1, None), (2, 3), (3, None)]
Explanation: Only one level with two children under the root.
EDGE CASE
Input: []
Output: Empty
Explanation: No nodes to connect.
Input: [1]
Output: [(1, None)]
Explanation: A single node has no right node, so `next` remains None.
2: M-atch
Match what this problem looks like to known categories of problems, e.g., Linked List or Dynamic Programming, and strategies or patterns in those categories.
For Populating Next Right Pointers, consider the following approaches:
- Level Order Traversal: Traverse the tree level by level, connecting nodes on the same level.
- Two-Pointer Technique: Use two pointers to move across the current level and the next level.
- BFS Queue Traversal: Use a queue to track nodes by level.
3: P-lan
Plan the solution with appropriate visualizations and pseudocode.
General Idea:
We traverse the tree level by level. For each node at the current level, connect its left
child to its right
child. If the node has a next
pointer, also connect its right
child to the left
child of the next node.
1) Start with the root of the tree.
2) Use a pointer `leftmost` to track the first node of each level.
3) For each node at the current level, connect:
a) `left.next = right`
b) If `next` exists, `right.next = next.left`
4) Move to the next level by setting `leftmost = leftmost.left`.
5) Continue until all levels are processed.
⚠️ Common Mistakes
- Forgetting to handle the last node on each level, where
next
should beNone
. - Not moving correctly to the next level using the leftmost node.
4: I-mplement
Implement the code to solve the algorithm.
class TreeNode:
def __init__(self, val=0, left=None, right=None, next=None):
self.val = val
self.left = left
self.right = right
self.next = next
def connect(root):
if not root:
return None
# Start with the root of the tree
leftmost = root
while leftmost.left: # Traverse while there are more levels
# Traverse the nodes at the current level
head = leftmost
while head:
# Connect the left child's next to the right child
head.left.next = head.right
# If there is a next node, connect the right child's next to the next node's left child
if head.next:
head.right.next = head.next.left
# Move to the next node at the same level
head = head.next
# Move to the next level
leftmost = leftmost.left
return root
5: R-eview
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
-
Input: [1, 2, 3, 4, 5, 6, 7]
- Connections: 2 -> 3, 4 -> 5, 5 -> 6, 6 -> 7
- Output: [(1, None), (2, 3), (3, None), (4, 5), (5, 6), (6, 7), (7, None)]
-
Input: [1, 2, 3]
- Connections: 2 -> 3
- Output: [(1, None), (2, 3), (3, None)]
-
Input: []
- Output: Empty
6: E-valuate
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N
is the number of nodes in the tree.
- Time Complexity:
O(N)
because we visit every node exactly once. - Space Complexity:
O(1)
since no extra space is used beyond the tree structure.