← Quiz: Binary Search Tree Operations: all 15 questions
Quiz: Binary Search Tree Operations · Question 7 of 15
What does this print? (code)
What does this print?
class Node:
def __init__(self, key, left=None, right=None):
self.key, self.left, self.right = key, left, right
def ok(n, lo=float('-inf'), hi=float('inf')):
if n is None:
return True
if not (lo < n.key < hi):
return False
return ok(n.left, lo, n.key) and ok(n.right, n.key, hi)
t = Node(20, Node(10, None, Node(15)), Node(30, Node(18), None))
print(ok(t))
- A.True
- B.False
- C.None
- D.Error
Answer & explanation
Correct answer: False
18 is in the right subtree of 20, so it must be larger than 20. The range check catches it.
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 →