Practice
1. You are given a sorted array of unique integers and need to construct a binary search tree with minimal height. Which approach guarantees the resulting tree is height-balanced and constructed in O(n) time?
easy
Solution
Step 1: Understand the problem constraints
The input is a sorted array, and the goal is to build a height-balanced BST efficiently.Step 2: Evaluate approaches
Inserting elements one by one leads to skewed trees and O(n^2) time. Greedy insertion of smallest elements does not guarantee balance. Merging BSTs is not straightforward and inefficient. The divide-and-conquer approach picks the middle element as root, ensuring balanced subtrees and O(n) time.Final Answer:
Option A -> Option AQuick Check:
Middle element chosen recursively -> balanced BST in O(n) time [OK]
Hint: Middle element recursion ensures balanced BST [OK]
Common Mistakes:
- Thinking inserting one by one is efficient
- Assuming greedy insertion balances tree
2. Given the following code for inserting a value into a BST, what will be the value of the variable
parent.val when inserting 5 into the BST rooted at 4 with left child 2 and right child 7?
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def insertIntoBST(root, val):
if root is None:
return TreeNode(val)
curr = root
parent = None
while curr:
parent = curr
if val < curr.val:
if curr.left is None:
curr.left = TreeNode(val)
return root
curr = curr.left
else:
if curr.right is None:
curr.right = TreeNode(val)
return root
curr = curr.right
return root
root = TreeNode(4)
root.left = TreeNode(2)
root.right = TreeNode(7)
val = 5
insertIntoBST(root, val)easy
Solution
Step 1: Trace insertion path
Start at root (4). Since 5 > 4, move right to node 7. Update parent to 4.Step 2: Check right child of 7
5 < 7, so move left. Left child of 7 is None, so insert here. Parent is now 7, but last parent before insertion was 4.Final Answer:
Option A -> Option AQuick Check:
Parent is last non-null node before insertion -> 4 [OK]
Hint: Parent tracks last node before insertion point [OK]
Common Mistakes:
- Confusing parent with current node
- Off-by-one in loop iteration
- Mistaking inserted value as parent
3. Consider the following Python code implementing BST validation using inorder traversal. What is the output of the program when run with the given test cases?
easy
Solution
Step 1: Trace first tree (2,1,3)
Inorder traversal yields [1,2,3], strictly increasing, so returns true.Step 2: Trace second tree (5,1,4 with children 3 and 6)
Inorder traversal yields [1,5,3,4,6], 3 < 5 violates BST property, so returns false.Final Answer:
Option A -> Option AQuick Check:
First tree valid BST, second invalid due to subtree violation [OK]
Hint: Inorder traversal must be strictly increasing [OK]
Common Mistakes:
- Assuming subtree root comparison is enough
- Confusing output order
- Missing strict inequality check
4. What is the time complexity of balancing a BST using the optimal approach of inorder traversal followed by iterative balanced BST construction from the collected nodes?
medium
Solution
Step 1: Analyze inorder traversal time
Inorder traversal visits each node once -> O(n).Step 2: Analyze balanced BST construction time
Rebuilding uses indices to pick middle nodes without extra sorting -> O(n).Step 3: Consider sorting complexity
Although inorder traversal produces sorted nodes, if sorting is mistakenly applied, complexity becomes O(n log n).Final Answer:
Option A -> Option AQuick Check:
Sorting nodes during traversal leads to O(n log n) [Trap]
Hint: Beware of sorting overhead; if sorting is done, complexity is O(n log n)
Common Mistakes:
- Assuming sorting is not needed, leading to O(n)
- Thinking recursive rebuild is O(n^2) due to repeated subtree calls
- Confusing traversal and construction complexities
5. What is the time complexity of the iterative inorder traversal approach to find the kth smallest element in a BST, where H is the height of the tree and k is the input parameter?
medium
Solution
Step 1: Analyze traversal steps
The algorithm traverses down to the leftmost node (cost O(H)) and then visits k nodes in inorder sequence.Step 2: Combine costs
Total time is O(H) to reach leftmost node plus O(k) to visit k nodes, so O(H + k).Final Answer:
Option A -> Option AQuick Check:
Traversal cost depends on height plus k nodes visited [OK]
Hint: Traversal cost includes height plus k nodes visited [OK]
Common Mistakes:
- Assuming O(n) always
- Ignoring height cost
- Assuming only k nodes visited without stack overhead
