Step 1: Understand Rust ownership and recursive types
Rust requires recursive types like BST nodes to use Box or other smart pointers to allocate on heap.
Step 2: Check each option
struct TreeNode { val: i32, left: Option, right: Option } uses Option which is correct. struct TreeNode { val: i32, left: TreeNode, right: TreeNode } and D use direct TreeNode which is invalid recursive type. struct TreeNode { val: i32, left: &TreeNode, right: &TreeNode } uses references without lifetimes, which is incorrect.
Start at root 5, set is empty. Check if complement 9-5=4 in set? No. Insert 5. Check left child 3: complement 9-3=6, 6 not in set. Insert 3. Check left 2: complement 9-2=7, 7 not in set. Insert 2. Check right 4: complement 9-4=5, 5 is in set, so return true.
Step 2: Confirm early return on finding pair
Since pair (5,4) sums to 9, function returns true immediately.
Final Answer:
true -> Option A
Quick Check:
Pair found (5+4=9) = true [OK]
Hint: Check if complement exists in set during DFS [OK]
Common Mistakes:
Not inserting current node before checking complement
Returning false too early
Ignoring left or right subtree
4. Identify the bug in this Rust function for Two Sum in BST:
D. Inserting current node value before checking complement causes false positive.
Solution
Step 1: Analyze order of operations in DFS
The code inserts current node value into set before checking if complement exists. This causes the complement check to always find current value itself, leading to false positive.
Step 2: Correct approach
Check if complement exists in set first, then insert current node value to avoid matching node with itself.
Final Answer:
Inserting current node value before checking complement causes false positive. -> Option D
Quick Check:
Check complement before insert to avoid false positive [OK]
Hint: Check complement before inserting current value [OK]
Common Mistakes:
Inserting before checking complement
Ignoring early return on true
Not handling None nodes
5. You want to optimize the Two Sum in BST problem to use O(h) space, where h is tree height. Which approach is best?
A BST inorder iterator yields sorted values. How can you use two iterators to solve Two Sum efficiently?
hard
A. Use a HashSet to store visited nodes and check complements during DFS.
B. Traverse BST in preorder and store all values in a vector, then use two pointers on vector.
C. Use two iterators: one from smallest to largest, another from largest to smallest, moving inward to find target sum.
D. Convert BST to linked list and then check pairs sequentially.
Solution
Step 1: Understand space constraints and BST properties
Storing all nodes uses O(n) space. Using BST inorder iterator yields sorted values with O(h) space.
Step 2: Use two iterators moving inward
One iterator moves from smallest to largest (inorder), another from largest to smallest (reverse inorder). Compare sums and move iterators accordingly to find target sum efficiently.
Step 3: Compare with other options
Traverse BST in preorder and store all values in a vector, then use two pointers on vector. uses O(n) space. Use a HashSet to store visited nodes and check complements during DFS. uses HashSet with O(n) space. Convert BST to linked list and then check pairs sequentially. is inefficient and uses extra space.
Final Answer:
Use two iterators: one from smallest to largest, another from largest to smallest, moving inward to find target sum. -> Option C
Quick Check:
Two BST iterators inward = O(h) space Two Sum [OK]
Hint: Use two BST iterators from ends moving inward [OK]
Common Mistakes:
Using full traversal storing all nodes
Ignoring space complexity
Not using reverse inorder iterator
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.