Embercode
✕
Binary Search TreesQuick quiz

Recovering and trimming

8 questions. Covers Trim a BST to a range in Python.

Questions 1 to 5 of 8

  1. 1. The trim_recursive function from this lesson is called on this tree with the range 2 to 8. What does the program print? ``python t = TreeNode(5, TreeNode(3, leaf(1), leaf(4)), TreeNode(8, leaf(7), leaf(9))) print(inorder(trim_recursive(t, 2, 8))) ``
  2. 2. The recover_bst function from the module's recovery lesson fixes the tree below. What does the program print? ``python u = TreeNode(5, TreeNode(8, leaf(1), leaf(4)), TreeNode(3, leaf(7), leaf(9))) recover_bst(u) print(inorder(u)) ``
  3. 3. The function below is meant to trim a BST to the range low to high, but it has a bug. Which statement describes it? ``python def trim_recursive(node, low, high): if node is None: return None if node.val < low: return None if node.val > high: return trim_recursive(node.left, low, high) node.left = trim_recursive(node.left, low, high) node.right = trim_recursive(node.right, low, high) return node ``
  4. 4. This change is made to trim_iterative. The while in the child-fixing loop is replaced with this if: ``python if node.left is not None and node.left.val < low: node.left = node.left.right `` The tree has root 10, its left child is 2, and 2's right child is 3. The range is 4 to 20. What goes wrong?
  5. 5. Put the steps of the recursive trim_recursive in the order the function checks them for one node.

    Use the arrows to put the steps in order.

    1. 1.If the node's value is less than low, return the trimmed right subtree
    2. 2.Otherwise, trim the left and right children, store the results as the new links, and return the node
    3. 3.If the node's value is greater than high, return the trimmed left subtree
    4. 4.If the node is None, return None

4 question(s) left on this page.