🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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))
  1. A.True
  2. B.False
  3. C.None
  4. 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 →