Embercode
✕
Binary Search TreesQuick quiz

Range sums and BST LCA

8 questions. Covers Lowest common ancestor in a Python BST.

Questions 1 to 5 of 8

  1. 1. What does this print? It uses the 14-node tree from the range sum lesson, built by inserting 50, 30, 70, 20, 40, 60, 80, 10, 25, 35, 45, 65, 75 and 85 in that order. It uses range_sum_pruned, reset and visits from that lesson. ``python reset() total = range_sum_pruned(root, 25, 65) print('sum=' + str(total) + ' visits=' + str(visits)) ``
  2. 2. What does this print? It uses the same tree and function as the previous question. The call reset() runs first, so seen starts empty. ``python reset() s = range_sum_pruned(root, 36, 39) print(s, seen) ``
  3. 3. Why does the pruned range sum skip the left subtree of a node whose value is below the low bound?
  4. 4. The lesson's lca_bst walk runs on the 9-node tree built from 6, 2, 8, 0, 4, 3, 5, 7 and 9. For the pair 3 and 5, what does lca_bst return, and how many nodes does it visit? Set visits to 0 before the call.
  5. 5. The function below should add the values inside [lo, hi]. On the 14-node tree from the range sum lesson, for the range [25, 65] it returns 200 instead of 350. ``python def buggy_sum(node, lo, hi): if node is None: return 0 if node.val < lo: return buggy_sum(node.left, lo, hi) if node.val > hi: return buggy_sum(node.right, lo, hi) return node.val + buggy_sum(node.left, lo, hi) + buggy_sum(node.right, lo, hi) `` Which change fixes it?

5 question(s) left on this page.