Unit 8 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:
collection1 = TreeNode("Hoya", None, TreeNode("Pothos", TreeNode("Pothos")))
Output:
["Pothos"]
Explanation:
"Pothos" appears twice, which is more frequent than any other plant.
EDGE CASE
Input:
collection2 = TreeNode("Hoya", TreeNode("Aloe", TreeNode("Aloe")), TreeNode("Pothos", TreeNode("Pothos")))
Output:
["Aloe", "Pothos"]
Explanation:
Both "Aloe" and "Pothos" appear twice, so both are returned in the list.
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 Binary Search Tree (BST) problems, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Perform an inorder traversal of the BST to get the nodes in sorted order. As we traverse, count the occurrences of each plant and track the maximum frequency observed. If a new maximum frequency is found, update the result list; if the same frequency is encountered, add the plant to the list.
1) Initialize variables:
- `current_val`: The current plant value being counted.
- `current_count`: The count of the current plant value.
- `max_count`: The maximum count observed so far.
- `most_common`: The list of plants with the highest frequency.
2) Define a helper function `inorder(node)` to perform an inorder traversal:
- If `node` is `None`, return.
- Recursively traverse the left subtree.
- Process the current node:
- If the current node's value is equal to `current_val`, increment `current_count`.
- Otherwise, reset `current_val` to the current node's value and set `current_count` to 1.
- If `current_count` is greater than `max_count`, update `max_count` and reset `most_common` to `[current_val]`.
- If `current_count` is equal to `max_count`, append `current_val` to `most_common`.
- Recursively traverse the right subtree.
3) Call `inorder(root)` starting from the root of the tree.
4) Return `most_common`.
⚠️ Common Mistakes
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 find_most_common(root):
if not root:
return []
# Helper function for inorder traversal
def inorder(node):
nonlocal current_val, current_count, max_count, most_common
if not node:
return
# Traverse the left subtree
inorder(node.left)
# Process current node
if node.val == current_val:
current_count += 1
else:
current_val = node.val
current_count = 1
if current_count > max_count:
max_count = current_count
most_common = [current_val]
elif current_count == max_count:
most_common.append(current_val)
# Traverse the right subtree
inorder(node.right)
# Initialize variables
current_val = None
current_count = 0
max_count = 0
most_common = []
# Start inorder traversal
inorder(root)
return most_common
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
- Example 1:
- Input:
`collection1 = TreeNode("Hoya", None, TreeNode("Pothos", TreeNode("Pothos")))`
- Execution:
- Perform inorder traversal: "Hoya" -> "Pothos" -> "Pothos".
- "Pothos" is encountered twice, more frequently than any other plant.
- Output:
`["Pothos"]`
- Example 2:
- Input:
`collection2 = TreeNode("Hoya", TreeNode("Aloe", TreeNode("Aloe")), TreeNode("Pothos", TreeNode("Pothos")))`
- Execution:
- Perform inorder traversal: "Aloe" -> "Aloe" -> "Hoya" -> "Pothos" -> "Pothos".
- Both "Aloe" and "Pothos" are encountered twice.
- Output:
`["Aloe", "Pothos"]`
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
of the tree. In a balanced tree, H
is O(log N)
, but in the worst case (skewed tree), it could be O(N)
.