Practice
helper with input nums = [1, 3, 5]?Solution
Step 1: Calculate mid index for initial call
For nums=[1,3,5], left=0, right=2, mid=(0+2)//2=1.Step 2: Identify root value
nums[mid] = nums[1] = 3, so root node value is 3.Final Answer:
Option A -> Option AQuick Check:
Mid index calculation picks 3 as root [OK]
- Choosing first or last element as root
- Off-by-one mid calculation
from typing import Optional
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def kthSmallest(root: Optional[TreeNode], k: int) -> int:
stack = []
current = root
count = 0
while current or stack:
while current:
stack.append(current)
current = current.left
current = stack.pop()
count += 1
if count == k:
return current.val
current = current.right
# Tree:
# 3
# / \
# 1 4
# \
# 2
root = TreeNode(3)
root.left = TreeNode(1)
root.right = TreeNode(4)
root.left.right = TreeNode(2)
print(kthSmallest(root, 2))
Solution
Step 1: Trace the inorder traversal steps
Stack initially empty, current=root(3). Push 3, move left to 1. Push 1, move left to None. Pop 1 (count=1), move right to 2.Step 2: Continue traversal to find kth=2
Push 2, move left None. Pop 2 (count=2), return 2 as kth smallest.Final Answer:
Option C -> Option CQuick Check:
Second smallest in BST is 2 [OK]
- Off-by-one counting of k
- Returning before incrementing count
Solution
Step 1: Move root to valid range
Root=3 is within [2,3], so no change.Step 2: Trim left subtree
Left child=1 < 2, so replace left child with its right subtree (None). Left child becomes None.Step 3: Trim right subtree
Right child=4 > 3, so replace right child with its left subtree (None). Right child becomes None.Final Answer:
Option B -> Option BQuick Check:
Root remains 3 with no children after trimming [OK]
- Assuming root changes when already in range
- Forgetting to trim subtrees correctly
def recoverTree(root):
first = second = prev = None
current = root
while current:
if not current.left:
if prev and prev.val > current.val:
if not first:
first = prev
second = current
else:
second = current
prev = current
current = current.right
else:
pred = current.left
while pred.right and pred.right != current:
pred = pred.right
if not pred.right:
pred.right = current
current = current.left
else:
pred.right = None
if prev and prev.val > current.val:
if not first:
first = prev
second = current
else:
second = current
prev = current
current = current.right
first.val, second.val = second.val, first.val
Solution
Step 1: Analyze detection of swapped nodes
The code sets first and second whenever a violation is found, but overwrites them without preserving the first violation's first node.Step 2: Identify the bug in swapping
The swap at the end assumes both first and second are found, but if only one violation is detected (adjacent nodes swapped), second may be incorrect or null, causing wrong swap.Final Answer:
Option A -> Option AQuick Check:
Swapping without confirming both nodes leads to incorrect recovery in adjacent swaps [OK]
- Overwriting first and second on multiple violations
- Swapping values prematurely after first violation
- Not restoring tree structure after Morris traversal
Solution
Step 1: Understand duplicates in BST inorder traversal
Inorder traversal visits nodes in sorted order including duplicates as separate nodes.Step 2: Check if modification is needed
Since duplicates appear as separate nodes, counting each node visited naturally counts duplicates separately without extra logic.Step 3: Evaluate other options
Skipping duplicates (B) or using a hash set (C) changes semantics. Removing duplicates after full traversal (D) is inefficient and breaks counting duplicates separately.Final Answer:
Option D -> Option DQuick Check:
Inorder traversal inherently counts duplicates separately [OK]
- Trying to skip duplicates
- Using extra space unnecessarily
- Assuming duplicates must be merged
