TIP102 Unit 9 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?
HAPPY CASE
Input:
cookies1 = TreeNode(5, TreeNode(2), TreeNode(-3))
Output:
[2, 4, -3]
Explanation:
The subtree sums are:
- Subtree rooted at node with value 2: sum = 2
- Subtree rooted at node with value -3: sum = -3
- Subtree rooted at node with value 5: sum = 4
All sums appear once, so they are all returned.
EDGE CASE
Input:
cookies2 = TreeNode(5, TreeNode(2), TreeNode(-5))
Output:
[2]
Explanation:
The subtree sums are:
- Subtree rooted at node with value 2: sum = 2
- Subtree rooted at node with value -5: sum = -5
- Subtree rooted at node with value 5: sum = 2
The sum 2 appears twice, which is more frequent than any other sum.
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 Subtree Sum problems, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea:
1) Define a helper function `postorder(node)` that:
- If `node` is `None`, return 0.
- Compute the sum of the left subtree.
- Compute the sum of the right subtree.
- Calculate the total sum of the current subtree as `node.val + left_sum + right_sum`.
- Record the frequency of this sum in a hash map.
- Return the total sum of the subtree.
2) In the main function `most_popular_cookie_combo(root)`:
- Initialize a hash map `sum_count` to store the frequency of each subtree sum.
- Call `postorder(root)` to populate the hash map.
- Identify the maximum frequency from the hash map.
- Collect and return all sums that have this maximum frequency.
⚠️ 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 most_popular_cookie_combo(root):
if not root:
return []
def postorder(node):
if not node:
return 0
# Compute the sum of the left and right subtrees and the current node's value
left_sum = postorder(node.left)
right_sum = postorder(node.right)
total_sum = node.val + left_sum + right_sum
# Record the frequency of the current subtree sum
if total_sum in sum_count:
sum_count[total_sum] += 1
else:
sum_count[total_sum] = 1
return total_sum
sum_count = {}
postorder(root)
# Find the maximum frequency
max_freq = max(sum_count.values())
# Collect all sums with the maximum frequency
result = [s for s, count in sum_count.items() if count == max_freq]
return result
# Example Usage:
cookies1 = TreeNode(5, TreeNode(2), TreeNode(-3))
cookies2 = TreeNode(5, TreeNode(2), TreeNode(-5))
print(most_popular_cookie_combo(cookies1)) # Output: [2, 4, -3]
print(most_popular_cookie_combo(cookies2)) # Output: [2]
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
- Example 1:
- Input:
`cookies1 = TreeNode(5, TreeNode(2), TreeNode(-3))`
- Execution:
- Traverse the tree using postorder traversal and calculate subtree sums.
- Record the frequency of each sum.
- Output:
[2, 4, -3]
- Example 2:
- Input:
`cookies2 = TreeNode(5, TreeNode(2), TreeNode(-5))`
- Execution:
- Traverse the tree using postorder traversal and calculate subtree sums.
- Identify that the sum 2 appears most frequently.
- Output:
[2]
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(N)
where N
is the number of nodes in the tree.
N
unique sums.O(N)
where N
is the number of nodes in the tree.
N
in the worst case (e.g., a skewed tree).
~~~