🎙 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 11 of 15

What does this print? (code)

What does this print?

class Node:
    def __init__(self, key):
        self.key, self.left, self.right = key, None, None

def insert(root, key):
    if root is None:
        return Node(key)
    if key < root.key:
        root.left = insert(root.left, key)
    else:
        root.right = insert(root.right, key)
    return root

def inorder(n):
    return inorder(n.left) + [n.key] + inorder(n.right) if n else []

root = None
for k in [8, 3, 10, 1, 6, 14]:
    root = insert(root, k)
print(inorder(root), root.left.right.key)
  1. A.[8, 3, 10, 1, 6, 14] 6
  2. B.[1, 3, 6, 8, 10, 14] 10
  3. C.[1, 3, 6, 8, 10, 14] 1
  4. D.[1, 3, 6, 8, 10, 14] 6
Answer & explanation

Correct answer: [1, 3, 6, 8, 10, 14] 6

Inorder of a BST is sorted. 6 is smaller than 8 but larger than 3, so it becomes the right child of 3.

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 →