Practice
Solution
Step 1: Understand the problem requirement
The problem requires updating each node's value to include all greater or equal values, which naturally aligns with processing nodes from largest to smallest.Step 2: Identify traversal order that processes nodes from largest to smallest
Reverse inorder traversal (right-root-left) visits nodes in descending order, allowing accumulation of sums as we go.Final Answer:
Option C -> Option CQuick Check:
Reverse inorder traversal accumulates sums correctly [OK]
- Using inorder traversal processes smaller nodes first, causing incorrect sums
- Using BFS does not guarantee order by node value
- Postorder traversal processes children before root, not suitable here
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]
- Thinking inserting one by one is efficient
- Assuming greedy insertion balances tree
second.val after the traversal completes?Solution
Step 1: Trace inorder traversal with Morris method
The inorder traversal visits nodes in order: 1, 3, 2, 4. The violations are detected when prev.val > current.val: first at (3,2).Step 2: Identify swapped nodes
First violation sets first=3, second=2. No second violation occurs, so second remains 2.Final Answer:
Option B -> Option BQuick Check:
Second node detected is the node with value 2 [OK]
- Confusing first and second nodes
- Missing second violation for adjacent swaps
- Off-by-one in traversal order
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]
- Assuming O(n) always
- Ignoring height cost
- Assuming only k nodes visited without stack overhead
Solution
Step 1: Understand DFS and set usage
The code checks if complement exists in seen before adding current node's value.Step 2: Identify bug in order of operations
Checking complement before adding current node's value misses pairs where both nodes are the same, or misses valid pairs if current node's value is needed for complement checks in children.Final Answer:
Option B -> Option BQuick Check:
Complement check must happen after adding current node's value to seen [OK]
- Checking complement before adding current node's value
- Not handling None nodes properly
- Returning False too early
