Unit 8 Session 2 Standard (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: ["Daisy", "Lily", "Tulip", "Rose", "Violet", "Lilac"], "Lilac"
Output: True
Explanation: "Lilac" exists in the tree as a node.
EDGE CASE
Input: ["Daisy", "Lily", "Tulip", "Rose", "Violet", "Lilac"], "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 Tree problems, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Recursively traverse the entire binary tree and check every node to see if it matches the target flower name.
1) If the current node is `None`, return `False`.
2) Check if the current node's value matches the target flower name:
- If it does, return `True`.
3) Recursively search the left and right subtrees.
4) If either subtree contains the target flower, return `True`; otherwise, return `False`.
⚠️ 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 non_bst_find_flower(root, name):
if root is None:
return False
if root.val == name:
return True
return non_bst_find_flower(root.left, name) or non_bst_find_flower(root.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("Daisy", TreeNode("Lily", TreeNode("Rose"), TreeNode("Violet")), TreeNode("Tulip", None, TreeNode("Lilac")))`, `name = "Lilac"`
- Execution:
- Start at "Daisy", "Lilac" != "Daisy", check both left and right children.
- Check "Lily", "Lilac" != "Lily", check both left and right children.
- Check "Tulip", "Lilac" != "Tulip", move to the right child "Lilac".
- Found "Lilac", return `True`.
- Output: `True`
- Example 2:
- Input: `garden = TreeNode("Daisy", TreeNode("Lily", TreeNode("Rose"), TreeNode("Violet")), TreeNode("Tulip", None, TreeNode("Lilac")))`, `name = "Sunflower"`
- Execution:
- Start at "Daisy", "Sunflower" != "Daisy", check both left and right children.
- Check "Lily", "Sunflower" != "Lily", check both left and right children.
- Check "Tulip", "Sunflower" != "Tulip", no match found, 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.
Time Complexity of non_bst_find_flower()
:
O(N)
, where N
is the number of nodes in the tree.Time Complexity of find_flower()
from Problem 2:
O(H)
, where H
is the height of the tree.H
is O(log N)
, making the search more efficient by exploiting the BST property.Unbalanced BST Search:
find_flower()
becomes O(N)
.H
equals the number of nodes N
, making the search process as inefficient as searching in a non-BST.