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?
True
if the flower is found in the tree, and False
otherwise.HAPPY CASE
Input: ["Rose", "Lilac", "Tulip", "Daisy", "Lily", None, "Violet"], "Lilac"
Output: True
Explanation: "Lilac" exists in the tree as a node.
EDGE CASE
Input: ["Rose", "Lilac", "Tulip", "Daisy", "Lily", None, "Violet"], "Sunflower"
Output: False
Explanation: "Sunflower" does not exist in the tree, so the output is `False`.
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 problems, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Use the properties of a BST to efficiently search for the target flower by recursively comparing the target value with the current node.
1) If the current node is `None`, return `False`.
2) Compare the target flower name with the current node's value:
- If they are equal, return `True`.
- If the target name is smaller, search the left subtree.
- If the target name is larger, search the right subtree.
3) Continue this process until the target is found or a leaf node is reached.
⚠️ 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_flower(inventory, name):
if inventory is None:
return False
if inventory.val == name:
return True
elif name < inventory.val:
return find_flower(inventory.left, name)
else:
return find_flower(inventory.right, name)
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
- Example 1:
- Input: `garden = TreeNode("Rose", TreeNode("Lilac", TreeNode("Daisy"), TreeNode("Lily")), TreeNode("Tulip", None, TreeNode("Violet")))`, `name = "Lilac"`
- Execution:
- Start at "Rose", "Lilac" < "Rose", move to the left child "Lily".
- "Lilac" < "Lily", move to the right child "Lilac".
- Found "Lilac", return `True`.
- Output: `True`
- Example 2:
- Input: `garden = TreeNode("Rose", TreeNode("Lilac", TreeNode("Daisy"), TreeNode("Lily")), TreeNode("Tulip", None, TreeNode("Violet")))`, `name = "Sunflower"`
- Execution:
- Start at "Rose", "Sunflower" > "Rose", move to the right child "Tulip".
- "Sunflower" > "Tulip", move to the right child "Violet".
- "Sunflower" > "Violet", no right child, return `False`.
- Output: `False`
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, this is O(N)
for a skewed tree, but in the average case for a balanced BST, it's O(log N)
.O(H)
due to the recursive call stack, which is also O(N)
in the worst case and O(log N)
in the average case.