Practice
Solution
Step 1: Understand BST property
The tree must maintain that left children are smaller and right children are larger than the node.Step 2: Identify insertion method
Traversing from root and choosing left or right child based on comparison ensures the new value is inserted at the correct leaf position without violating BST properties.Final Answer:
Option C -> Option CQuick Check:
Insertion follows BST ordering rules [OK]
- Using BFS ignores BST ordering
- Inorder traversal is for traversal, not insertion
- Heap insertion is unrelated to BST
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
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def searchBST(root, val):
current = root
while current:
if current.val == val:
return current
elif val < current.val:
current = current.left
else:
current = current.right
return None
root = TreeNode(4, TreeNode(2, TreeNode(1), TreeNode(3)), TreeNode(7))
result = searchBST(root, 3)
print(result.val if result else 'null')Solution
Step 1: Trace the search path
Start at root (4). Since 3 < 4, move to left child (2). Since 3 > 2, move to right child (3).Step 2: Check node value
Current node value is 3, which matches the search value, so return this node.Final Answer:
Option D -> Option DQuick Check:
Search follows BST property correctly and finds node with value 3 [OK]
- Returning wrong node value due to off-by-one traversal
Solution
Step 1: Identify traversal path length
The algorithm moves from root down one path until the split point, visiting at most h nodes.Step 2: Understand h vs n
Height h can be up to n in worst case (skewed tree), but the question asks for optimal algorithm time complexity, which assumes balanced BST where h = log n.Final Answer:
Option A -> Option AQuick Check:
Traversal limited to one root-to-node path in balanced BST [OK]
- Confusing worst-case O(n) with average-case O(log n)
- Assuming constant time due to simple comparisons
- Ignoring that recursion stack space is not used here
Solution
Step 1: Understand duplicates in BST
Duplicates can appear on either left or right subtree depending on BST definition.Step 2: Modify search to find all matches
After finding a node with val, continue searching both subtrees to find all duplicates.Step 3: Avoid full traversal
Using BST property to prune search still possible by checking subtrees where duplicates may exist.Final Answer:
Option B -> Option BQuick Check:
Continuing search after first match finds all duplicates [OK]
- Stopping at first match or ignoring BST property completely
