Unit 8 Session 2 Standard (Click for link to problem statements)
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: ["FiddleLeafFig", "Monstera", "SnakePlant"], "Aloe"
Output: The updated BST with "Aloe" as the left child of "Monstera".
Explanation: The tree is updated to include "Aloe" while maintaining the BST properties.
EDGE CASE
Input: [], "Aloe"
Output: A new tree with "Aloe" as the root.
Explanation: An empty tree should return a new tree with only the "Aloe" node.
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: Traverse the BST to find the appropriate location for the new node based on the plant's name. Insert the new node while maintaining the BST properties.
1) If the current node (`collection`) is `None`, create a new `TreeNode` with the given plant name.
2) If the new plant name is less than the current node's value, recurse on the left subtree.
3) If the new plant name is greater than or equal to the current node's value, recurse on the right subtree.
4) After the recursive call, return the current node to link the updated subtree to the original tree.
5) Return the root of the updated tree.
⚠️ 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 add_plant(collection, name):
# Base case: If the tree is empty, create a new node with the plant name
if collection is None:
return TreeNode(name)
# Recursive case: Traverse the tree to find the correct insertion point
if name < collection.val:
collection.left = add_plant(collection.left, name)
else:
collection.right = add_plant(collection.right, name)
return collection
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
- Example 1:
- Input: `collection = TreeNode("FiddleLeafFig", TreeNode("Monstera"), TreeNode("SnakePlant"))`, `name = "Aloe"`
- Execution:
- Start at "FiddleLeafFig", "Aloe" < "FiddleLeafFig", move to the left child "Monstera".
- "Aloe" < "Monstera", left child is `None`, insert "Aloe" as the left child.
- Output: The tree now has "Aloe" as the left child of "Monstera".
- Example 2:
- Input: `collection = None`, `name = "Aloe"`
- Execution: Tree is empty, so "Aloe" becomes the root.
- Output: The tree now has a single node with "Aloe".
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N
represents the number of nodes in the tree.
O(H)
where H
is the height of the tree. In the worst case of a skewed tree, this could be O(N)
.O(H)
due to the recursive call stack. This could be O(N)
in the worst case for a skewed tree.