Unit 8 Session 2 (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(20, TreeNode(10), TreeNode(30)), value = 25
Output: TreeNode structure with new TreeNode(25) under TreeNode(30).
Explanation: The value 25 is inserted correctly according to BST rules, fitting into the tree structure.
EDGE CASE
Input: TreeNode(20, TreeNode(10), TreeNode(30)), value = 20
Output: TreeNode structure with new TreeNode(20) inserted to the left of the existing TreeNode(20).
Explanation: As 20 is a duplicate, it's added to the left subtree of the existing 20 to maintain consistency.
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 tree insertion problem with a twist on handling duplicates, often seen in systems that require maintaining historical data or versions in order.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Insert a new value into the binary search tree, with any duplicate values inserted to the left of the existing value.
1) If the tree is empty, insert the new node here.
2) If the new value is less than or equal to the current node's value, insert to the left.
3) If the new value is greater, insert to the right.
4) Return the unchanged root node, ensuring the tree remains connected.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
def insert_with_duplicates(root, value):
"
Insert a new node with the given `value` into the binary search tree rooted at `root`,
placing duplicates in the left subtree.
"
if root is None:
return TreeNode(value)
if value <= root.val:
root.left = insert_with_duplicates(root.left, value)
else:
root.right = insert_with_duplicates(root.right, value)
return root
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(h)
where h is the height of the tree, which could range from O(log n)
in a balanced tree to O(n)
in a skewed tree.O(h)
due to recursion, potentially reaching O(n)
in a skewed scenario.