-
Notifications
You must be signed in to change notification settings - Fork 242
Foraging Berries
Unit 8 Session 1 Standard (Click for link to problem statements)
- 💡 Difficulty: Easy
- ⏰ Time to complete: 15 mins
- 🛠️ Topics: Binary Tree, Tree Traversal, Recursion
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 does the threshold represent?
- The threshold is the minimum number of berries required on a branch to consider it for harvesting.
- How should the function behave if there are no branches exceeding the threshold?
- The function should return 0 if no branches exceed the threshold.
HAPPY CASE
Input: Binary tree with nodes [4, 10, 6, 5, 8, 20] and threshold 6
Output: 38
Explanation: Nodes with values greater than 6 are 8, 10, and 20. Their sum is 38.
EDGE CASE
Input: Binary tree with nodes [4, 10, 6, 5, 8, 20] and threshold 30
Output: 0
Explanation: No nodes have a value greater than 30.
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 Summation problems, we want to consider the following approaches:
- Binary Tree Traversal: Traverse the tree to accumulate the sum of node values that exceed the threshold.
- Recursion: Use recursion to explore each node and sum values conditionally based on the threshold.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Traverse the tree, checking each node's value against the threshold and summing those that exceed it.
1) If the current node is None, return 0.
2) Recursively calculate the sum of berries in the left subtree.
3) Recursively calculate the sum of berries in the right subtree.
4) If the current node's value exceeds the threshold, include it in the sum.
5) Return the total sum of berries that exceed the threshold.
- Not accounting for cases where no nodes exceed the threshold.
- Forgetting to include nodes that meet or exceed the threshold in the sum.
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 harvest_berries(root, threshold):
if root is None:
return 0
# Sum the berries in the left and right subtrees
left_sum = harvest_berries(root.left, threshold)
right_sum = harvest_berries(root.right, threshold)
# Include the current node's berries if it exceeds the threshold
if root.val > threshold:
return root.val + left_sum + right_sum
else:
return left_sum + right_sum
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 [4, 10, 6, 5, 8, 20] and threshold 6
- Expected Output: 38
- Input 2: Binary tree with nodes [4, 10, 6, 5, 8, 20] and threshold 30
- Expected Output: 0
- Verify that the outputs are correct.
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N
represents the number of nodes in the binary tree.
-
Time Complexity:
O(N)
because the algorithm needs to visit each node to evaluate whether it exceeds the threshold. -
Space Complexity:
O(H)
whereH
is the height of the tree, due to the recursive call stack.