TIP102 Unit 9 Session 2 Standard (Click for link to problem statements)
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:
themes = ["👻", "👻", "🧛🏾", "👻", "🧛🏾", None, "🧛🏾"]
hotel = build_tree(themes)
Output:
3
Explanation:
There are three distinct clusters: one cluster of "👻" and two clusters of "🧛🏾".
EDGE CASE
Input:
themes = ["👻", "👻", "👻", "👻", "👻"]
hotel = build_tree(themes)
Output:
1
Explanation:
There is only one distinct cluster, as all rooms have the same theme.
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 Cluster Counting in Trees problems, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Use DFS to traverse the tree and count distinct clusters. A new cluster is identified when a node has a different theme from its parent. The DFS function will recursively check the left and right subtrees to accumulate the total number of clusters.
1) Define a DFS function `dfs(node, parent_val)`:
- If `node` is `None`, return `0` (no clusters).
- Check if the current node starts a new cluster:
- If the node's value differs from its parent's value, it is the start of a new cluster.
- Recursively count clusters in the left and right subtrees:
- Pass the current node's value as the parent value to the child nodes.
- Return the total number of clusters found.
2) In the main function `count_clusters(hotel)`:
- Start DFS with the root node. The root itself is always the start of a new cluster.
- Return the result from the DFS function.
⚠️ Common Mistakes
None
.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 count_clusters(hotel):
def dfs(node, parent_val):
if node is None:
return 0
# Check if this node starts a new cluster
cluster_count = 1 if node.val != parent_val else 0
# Recursively count clusters in left and right subtrees
cluster_count += dfs(node.left, node.val)
cluster_count += dfs(node.right, node.val)
return cluster_count
# Start DFS with root node; root itself is always a new cluster
return dfs(hotel, None)
# Example Usage:
themes = ["👻", "👻", "🧛🏾", "👻", "🧛🏾", None, "🧛🏾"]
hotel = build_tree(themes)
print(count_clusters(hotel)) # 3
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
- Example 1:
- Input:
`themes = ["👻", "👻", "🧛🏾", "👻", "🧛🏾", None, "🧛🏾"]`
`hotel = build_tree(themes)`
- Execution:
- Perform DFS to traverse the tree.
- Identify distinct clusters by comparing node values to parent values.
- Output:
3
- Example 2:
- Input:
`themes = ["👻", "👻", "👻", "👻", "👻"]`
`hotel = build_tree(themes)`
- Execution:
- Perform DFS to traverse the tree.
- Identify distinct clusters by comparing node values to parent values.
- Output:
1
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)
.
~~~