Practice
Solution
Step 1: Trace reverse inorder traversal order
Nodes visited in order: 3, 2, 1.Step 2: Accumulate sums and update nodes
acc_sum=0 initially. - Visit 3: acc_sum=3, node.val=3 - Visit 2: acc_sum=3+2=5, node.val=5 - Visit 1: acc_sum=5+1=6, node.val=6 Root node was 2, updated to 5.Final Answer:
Option A -> Option AQuick Check:
Root node value after update is 5 [OK]
- Confusing traversal order and updating nodes too early
- Off-by-one errors in accumulating sums
- Misidentifying which node is root after updates
Solution
Step 1: Identify number of nodes visited
Each node is visited exactly once during traversal.Step 2: Analyze stack operations and overall traversal
While each node is visited once, the explicit stack can hold up to h nodes, and each push/pop operation can be considered O(1). However, in the worst case (unbalanced tree), the height h can be up to n, making the complexity O(n * h).Final Answer:
Option B -> Option BQuick Check:
Worst-case complexity depends on tree height [OK]
- Assuming stack operations multiply complexity by height unnecessarily
- Confusing recursion stack space with time complexity
- Thinking balanced BST implies O(n log n) time
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 worst-case scenario
Worst case occurs when all nodes fall within the range or pruning is ineffective, so all n nodes are visited.Step 2: Analyze time complexity
Each node is processed once, so time complexity is O(n). Stack operations are O(1) amortized per node.Final Answer:
Option C -> Option CQuick Check:
Worst case is full traversal, O(n) time [OK]
- Assuming pruning always reduces to O(log n)
- Confusing height h with number of nodes n
Solution
Step 1: Understand how duplicates affect sum accumulation
Duplicates must be included in the sum for nodes with equal values to maintain correctness.Step 2: Modify accumulation logic
Accumulate sum including all nodes with values >= current node before updating node.val during reverse inorder traversal.Final Answer:
Option D -> Option DQuick Check:
Including equal values in sum ensures correct Greater Tree with duplicates [OK]
- Changing traversal order breaks sum logic
- Skipping duplicates causes incorrect sums
- Using extra data structures unnecessarily increases complexity
