Unit 8 Session 1 (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: TreeNode(10, TreeNode(5), TreeNode(15)), value = 15
Output: True
Explanation: The value 15 is found in the right subtree of the root.
EDGE CASE
Input: TreeNode(10, TreeNode(5), TreeNode(15)), value = 20
Output: False
Explanation: The value 20 is not found in the tree.
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.
This is a binary search tree (BST) search problem, which typically utilizes the BST property to efficiently find a node.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Utilize the properties of a binary search tree to efficiently find the node.
1) Start at the root.
2) If the current node is None, return False.
3) If the current node's value matches the target, return True.
4) If the target value is less than the current node's value, search the left subtree.
5) Otherwise, search the right subtree.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
def find_bst(root, value):
"
Search for a node with the given `value` in the binary search tree rooted at `root`.
Return True if the value is found, otherwise return False.
"
if root is None:
return False
if root.val == value:
return True
elif value < root.val:
return find_bst(root.left, value)
else:
return find_bst(root.right, value)
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
O(log n)
on average, where n is the number of nodes in the tree. The height of the tree, due to its balanced nature, allows for log-scale depth traversal.O(log n)
due to the recursion depth, which is the height of the tree in the balanced case.