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
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
Solution
Step 1: Understand pruning logic
If node.val < low, left subtree nodes are smaller and cannot be in range, so left subtree should be skipped.Step 2: Identify incorrect line
Appending node.left when node.val < low causes unnecessary traversal of left subtree, violating pruning. It should append node.right instead.Final Answer:
Option A -> Option AQuick Check:
Correct pruning requires appending node.right only when node.val < low [OK]
- Appending wrong subtree when pruning
- Forgetting to check null nodes
Solution
Step 1: Understand duplicates in BST with relaxed property
Inorder traversal still produces nodes in non-decreasing order preserving duplicates order.Step 2: Rebuild balanced BST using middle node selection
Choosing middle node as root maintains balance and respects duplicates order.Step 3: Avoid sorting or preorder traversal
Sorting is unnecessary and preorder breaks sorted order; left middle bias skews balance.Final Answer:
Option C -> Option CQuick Check:
Inorder traversal + middle node rebuild works with duplicates [OK]
- Using preorder traversal losing sorted order
- Sorting nodes again causing unnecessary overhead
- Biasing middle index causing unbalanced tree
def insertIntoBST(root, val):
if root is None:
return TreeNode(val)
curr = root
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
Solution
Step 1: Understand duplicate insertion rule
Duplicates must go to the left subtree, so values equal to current node should be treated as less or equal.Step 2: Modify comparison operator
Changing condition to val <= curr.val ensures duplicates follow left subtree insertion path, preserving BST property.Final Answer:
Option A -> Option AQuick Check:
Duplicates inserted left by using <= comparison [OK]
- Inserting duplicates right breaks problem requirement
- Inserting duplicates as new roots breaks BST
- Ignoring duplicates causes infinite recursion
