TIP102 Unit 9 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:
rooms = [4, 7, 13, 666, 1313]
Output:
[13, 7, 1313, 4, None, 666]
Explanation:
The array is converted into a height-balanced BST as follows:
13
/ \
7 1313
/ /
4 666
EDGE CASE
Input:
rooms = [1]
Output:
[1]
Explanation:
The array contains only one element, so the BST will have a single 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:
1) Define a helper function that takes a subarray defined by its start and end indices.
2) Base Case: If the start index is greater than the end index, return `None`.
3) Calculate the middle index of the current subarray.
4) Create a new `TreeNode` with the value at the middle index.
5) Recursively build the left subtree using the left half of the current subarray.
6) Recursively build the right subtree using the right half of the current subarray.
7) Return the root node of the BST.
8) In the main function, call the helper function with the entire array.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def array_to_bst(rooms):
if not rooms:
return None
def build_bst(start, end):
if start > end:
return None
mid = (start + end) // 2
root = TreeNode(rooms[mid])
root.left = build_bst(start, mid - 1)
root.right = build_bst(mid + 1, end)
return root
return build_bst(0, len(rooms) - 1)
# Example Usage:
rooms = [4, 7, 13, 666, 1313]
print_tree(array_to_bst(rooms))
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
- Example 1:
- Input:
`rooms = [4, 7, 13, 666, 1313]`
- Execution:
- The middle element is 13, which becomes the root.
- The left subtree is built from [4, 7] and the right subtree from [666, 1313].
- Output:
[13, 7, 1313, 4, None, 666]
- Example 2:
- Input:
`rooms = [1]`
- Execution:
- The array contains a single element, so the root is 1.
- Output:
[1]
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
O(N)
where N
is the number of elements in the array.
O(N)
for the recursion stack space in the worst case of an unbalanced tree.
O(log N)
, but in the worst case of an unbalanced tree, it could be O(N)
.
~~~