TIP102 Unit 9 Session 2 Standard (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?
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.
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:
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
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]
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]
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
O(N)
where N
is the number of nodes in the tree.
O(H)
where H
is the height of the tree.
H
is O(log N)
, but in the worst case (skewed tree), it could be O(N)
.
~~~