← Quiz: Binary Search Tree Operations: all 15 questions
Quiz: Binary Search Tree Operations · Question 13 of 15
What does this print? (code)
What does this print?
keys = {8: (3, 10), 3: (1, 6), 10: (None, 14), 1: (None, None), 6: (None, None), 14: (None, None)}
def search(node, target, steps=1):
if node is None:
return None
if node == target:
return steps
left, right = keys[node]
return search(left if target < node else right, target, steps + 1)
print(search(8, 6), search(8, 7))
- A.2 None
- B.3 None
- C.3 4
- D.6 None
Answer & explanation
Correct answer: 3 None
Searching for 6 visits 8, 3, 6. Searching for 7 goes 8 → 3 → 6 → empty right child, so it returns None.
Can you get the rest right without peeking?
Take the timed Quiz: Binary Search Tree Operations quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →