Practice
total returned by rangeSumBST?Solution
Step 1: Trace initial stack and total
Start with stack=[10], total=0.Step 2: Pop 10 (in range), add 10, push left(5) and right(15)
total=10, stack=[5,15]Step 3: Pop 15 (in range), add 15, push left(null) and right(null)
total=25, stack=[5,null,null]Step 4: Pop null (skip), then pop null (skip), then pop 5 (less than low=7), push right(null)
total=25, stack=[null]Step 5: Pop null (skip), stack empty, return total=25
5 is less than low, so it is skipped and not added.Final Answer:
Option A -> Option AQuick Check:
Sum includes 10 and 15 only, total=25 [OK]
- Adding nodes outside range
- Forgetting to push children after adding node
def lowestCommonAncestor(root, p, q):
current = root
while current:
if p.val < current.val and q.val < current.val:
current = current.left
elif p.val > current.val and q.val > current.val:
current = current.right
else:
return current
Solution
Step 1: Analyze comparison operators
Using '<=' and '>=' causes the loop to move past the node when p or q equals current, missing the case where current is ancestor of itself.Step 2: Correct operator usage
Changing to '<' and '>' ensures the loop stops at the correct ancestor node, including when one node is ancestor of the other.Final Answer:
Option A -> Option AQuick Check:
Inclusive comparisons skip valid ancestor nodes [OK]
- Assuming ancestor must be strictly above nodes
- Ignoring null root edge cases
- Returning too early without validation
Solution
Step 1: Identify time complexity
The algorithm visits each node exactly once in inorder traversal, so time is O(n).Step 2: Identify space complexity
Space is O(h) due to recursion stack, where h is the height of the tree; no extra arrays store all nodes.Final Answer:
Option A -> Option AQuick Check:
Linear time and stack space proportional to tree height [OK]
- Confusing recursion stack space with storing all nodes
- Assuming O(n log n) due to sorting
- Ignoring recursion stack space
Solution
Step 1: Identify BST strict inequality
BST requires node.val to be strictly greater than previous inorder value; using < instead of <= allows duplicates.Step 2: Understand impact of bug
Using node.val < prev[0] misses the case when node.val == prev[0], incorrectly allowing duplicates and invalid BSTs.Final Answer:
Option C -> Option CQuick Check:
Strict inequality check is essential to reject duplicates [OK]
- Allowing duplicates by using < instead of <=
- Not resetting prev between calls
- Only comparing immediate children
Solution
Step 1: Understand duplicates in BST
Duplicates can be on either left or right subtree depending on insertion rules, so pruning must consider equality carefully.Step 2: Adjust pruning for duplicates
When node.val equals low or high, both subtrees may contain duplicates within range, so both should be explored.Step 3: Avoid removing pruning entirely
Removing pruning loses efficiency; using a hash set is unnecessary as nodes are unique objects.Final Answer:
Option D -> Option DQuick Check:
Pruning adjusted for equality handles duplicates efficiently [OK]
- Removing pruning loses efficiency
- Assuming duplicates only on one side
