Escaping the Sea Caves II - codepath/compsci_guides GitHub Wiki
Unit 8 Session 1 Standard (Click for link to problem statements)
Unit 8 Session 1 Advanced (Click for link to problem statements)
Problem Highlights
- 💡 Difficulty: Easy
- ⏰ Time to complete: 10 mins
- 🛠️ Topics: Binary Tree, Tree Traversal, Recursion
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 the leftmost path in a binary tree?
- The leftmost path is the path that starts from the root and continues to follow the left child at each step until a leaf node is reached.
- How should the function behave if there are no left children?
- The function should return a list with only the root node's value.
HAPPY CASE
Input: Binary tree with nodes ["CaveA", "CaveB", "CaveD", "CaveE", "CaveC", "CaveF"]
Output: ["CaveA", "CaveB", "CaveD"]
Explanation: The leftmost path is ["CaveA", "CaveB", "CaveD"].
EDGE CASE
Input: Binary tree with nodes ["CaveA", "CaveB", "CaveC"] where only the root has children.
Output: ["CaveA"]
Explanation: The root has no left children, so the path only includes the root.
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 Tree Path problems, we want to consider the following approaches:
- Binary Tree Traversal: Traverse the tree following the left child at each step to collect the values.
- Recursion: Use recursion to traverse down the leftmost path and collect the values.
3: P-lan
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Use recursion to follow the left child at each step, collecting the values along the way.
1) If the current node is None, return an empty list.
2) Recursively find the leftmost path starting from the left child.
3) Include the current node's value and concatenate it with the left path.
4) Return the resulting list of values.
⚠️ Common Mistakes
- Not correctly handling cases where the tree has no left children, leading to incomplete paths.
- Assuming that every node has a left child, which could lead to errors during recursion.
4: I-mplement
Implement the code to solve the algorithm.
class TreeNode:
def __init__(self, value, left=None, right=None):
self.val = value
self.left = left
self.right = right
def leftmost_path(root):
if root is None:
return []
# Recursively find the leftmost path starting from the left child
left_path = leftmost_path(root.left)
# Include the current node's value and concatenate with the left path
return [root.val] + left_path
5: R-eview
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
- Test with the examples given:
- Input 1: Binary tree with nodes ["CaveA", "CaveB", "CaveD", "CaveE", "CaveC", "CaveF"]
- Expected Output: ["CaveA", "CaveB", "CaveD"]
- Input 2: Binary tree with nodes ["CaveA", "CaveB", "CaveC"] where only the root has children.
- Expected Output: ["CaveA"]
- Verify that the outputs match the expected results.
6: E-valuate
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume H
represents the height of the binary tree.
- Time Complexity:
O(H)
because the algorithm only traverses the height of the tree. - Space Complexity:
O(H)
because the recursion stack will use space proportional to the height of the tree.