← Quiz: Binary Search Tree Operations: all 15 questions
Quiz: Binary Search Tree Operations · Question 2 of 15
What does this print? (code)
What does this print?
class Node:
def __init__(self, k):
self.key, self.left, self.right = k, None, None
def insert(r, k):
if r is None:
return Node(k)
if k < r.key:
r.left = insert(r.left, k)
else:
r.right = insert(r.right, k)
return r
def inorder(r):
return inorder(r.left) + [r.key] + inorder(r.right) if r else []
root = None
for k in [8, 3, 10, 1, 6]:
root = insert(root, k)
print(inorder(root))
- A.[8, 3, 10, 1, 6]
- B.[8, 3, 1, 6, 10]
- C.[1, 6, 3, 10, 8]
- D.[1, 3, 6, 8, 10]
Answer & explanation
Correct answer: [1, 3, 6, 8, 10]
Inorder traversal of any BST lists the keys in sorted order.
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 →