Pumpkin Patch Path - codepath/compsci_guides GitHub Wiki
TIP102 Unit 9 Session 2 Standard (Click for link to problem statements)
Problem Highlights
- 💡 Difficulty: Medium
- ⏰ Time to complete: 20-25 mins
- 🛠️ Topics: Trees, Recursion, Pathfinding
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 structure of the tree?
- The tree is a binary tree where each node represents a section of a pumpkin patch with a certain number of pumpkins.
- What operation needs to be performed?
- The function needs to find the root-to-leaf path that yields the largest number of pumpkins.
- What should be returned?
- The function should return a list of node values along the maximum pumpkin path.
HAPPY CASE
Input:
pumpkin_quantities = [7, 3, 10, 1, None, 5, 15]
root1 = build_tree(pumpkin_quantities)
Output:
[7, 10, 15]
Explanation:
The path from the root (7) through 10 to 15 yields the highest number of pumpkins.
EDGE CASE
Input:
pumpkin_quantities = [12, 3, 8, 4, 50, None, 10]
root2 = build_tree(pumpkin_quantities)
Output:
[12, 3, 50]
Explanation:
The path from the root (12) through 3 to 50 yields the highest number of pumpkins.
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 Pathfinding in Trees problems, we want to consider the following approaches:
- Recursion: Recursion is a natural fit for tree problems, allowing us to explore different paths from the root to the leaves.
- Depth-First Search (DFS): DFS can be used to traverse the tree and find the path that yields the maximum sum of pumpkin quantities.
3: P-lan
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Recursively explore all root-to-leaf paths. At each node, determine the maximum pumpkin path from the current node by comparing the paths through the left and right children. Accumulate the node values along the path with the maximum sum.
1) Define a recursive function `max_pumpkins_path(root)`:
- If `root` is `None`, return an empty list (no path).
- Recursively find the maximum pumpkin path for the left and right subtrees.
- Compare the sums of the paths obtained from the left and right subtrees:
- If the left path has a higher sum, return `[root.val] + left_path`.
- If the right path has a higher sum, return `[root.val] + right_path`.
- Return the path with the highest sum.
⚠️ Common Mistakes
- Forgetting to handle the base case where the tree is empty.
- Not properly accumulating the node values along the path when comparing the left and right paths.
4: I-mplement
Implement the code to solve the algorithm.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def max_pumpkins_path(root):
if root is None:
return []
left_path = max_pumpkins_path(root.left)
right_path = max_pumpkins_path(root.right)
if sum([root.val] + left_path) > sum([root.val] + right_path):
return [root.val] + left_path
else:
return [root.val] + right_path
# Example Usage:
pumpkin_quantities = [7, 3, 10, 1, None, 5, 15]
root1 = build_tree(pumpkin_quantities)
pumpkin_quantities = [12, 3, 8, 4, 50, None, 10]
root2 = build_tree(pumpkin_quantities)
print(max_pumpkins_path(root1)) # [7, 10, 15]
print(max_pumpkins_path(root2)) # [12, 3, 50]
5: R-eview
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
- Example 1:
- Input:
`pumpkin_quantities = [7, 3, 10, 1, None, 5, 15]`
`root1 = build_tree(pumpkin_quantities)`
- Execution:
- Recursively find the maximum pumpkin path from the root.
- Compare the sums of the left and right paths.
- Return the path with the highest sum.
- Output:
[7, 10, 15]
- Example 2:
- Input:
`pumpkin_quantities = [12, 3, 8, 4, 50, None, 10]`
`root2 = build_tree(pumpkin_quantities)`
- Execution:
- Recursively find the maximum pumpkin path from the root.
- Compare the sums of the left and right paths.
- Return the path with the highest sum.
- Output:
[12, 3, 50]
6: E-valuate
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Time Complexity:
- Time Complexity:
O(N)
whereN
is the number of nodes in the tree.- Explanation: Each node is visited exactly once to find the maximum pumpkin path.
Space Complexity:
- Space Complexity:
O(H)
whereH
is the height of the tree.- Explanation: The recursion stack will use space proportional to the height of the tree. In a balanced tree,
H
isO(log N)
, but in the worst case (skewed tree), it could beO(N)
.
- Explanation: The recursion stack will use space proportional to the height of the tree. In a balanced tree,