Unit 12 Session 2 Advanced (Click for link to problem statements)
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?
What does the next
pointer do?
next
pointer is set to None
.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.
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:
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
next
should be None
.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
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]
Input: [1, 2, 3]
Input: []
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.
O(N)
because we visit every node exactly once.O(1)
since no extra space is used beyond the tree structure.