iq.lab
Python starts when a code cell comes near or you run one
08 Recursion and trees

Checkpoint

Recursion and trees

These twelve questions mix all five lessons: the call stack, the three depth-first orders, answers passed up and down, the level-by-level walk and the BST promise. Every tree is new, so draw it on paper before you answer: read the build_tree list in pairs, one pair per real node, and remember that a None takes no children. The last question asks for code.

  1. Question 1 of 12.

    Each call prints after the smaller call has returned. Type exactly what this prints.

    def climb(n):
        if n == 0:
            print("ground")
            return 1
        below = climb(n - 1)
        print(n, below)
        return below * 2
    
    print(climb(3))
    
  2. Question 2 of 12.

    One walk appends each value to pre when its call starts and to post when its call is about to finish. Type exactly what it prints.

    def orders(node, pre, post):
        if node is None:
            return
        pre.append(node.val)
        orders(node.left, pre, post)
        orders(node.right, pre, post)
        post.append(node.val)
    
    pre, post = [], []
    orders(build_tree([5, None, 8, 6, 9, None, 7]), pre, post)
    print(pre)
    print(post)
    
  3. Question 3 of 12.

    This is the round loop recording the last node of each level. Type exactly what it prints.

    from collections import deque
    
    def right_view(root):
        view = []
        queue = deque([root])
        while queue:
            size = len(queue)
            for i in range(size):
                node = queue.popleft()
                if i == size - 1:
                    view.append(node.val)
                if node.left:
                    queue.append(node.left)
                if node.right:
                    queue.append(node.right)
        return view
    
    print(right_view(build_tree([1, 2, 3, None, 5, None, None, 6])))
    
  4. Question 4 of 12.

    Here is build_tree([10, 5, 15, None, None, 6, 20]):

          10
         /  \
        5    15
            /  \
           6    20
    

    Which statement is true?

  5. Question 5 of 12.

    Here is build_tree([1, 2, 3, 4, 5, None, None, 6, None, None, 7, 8, None, None, 9]):

              1
            /   \
           2     3
          / \
         4   5
        /     \
       6       7
      /         \
     8           9
    

    What does diameter from the lesson return for it: the number of edges (links) on the longest path between any two nodes? Type a whole number.

    edges

    Type a number: 12, -1, 0.5 and 3/4 all work.

  6. Question 6 of 12.

    fib(n) returns n when n <= 1 and otherwise fib(n - 1) + fib(n - 2), with no memo. What are its running time and its extra space?

  7. Question 7 of 12.

    Here is the BST build_tree([20, 10, 30, 5, 15, 25, 35, None, None, 12]):

              20
            /    \
          10      30
         /  \    /  \
        5   15  25   35
            /
          12
    

    What is its 3rd smallest value (k = 3)? Type the value.

    value

    Type a number: 12, -1, 0.5 and 3/4 all work.

  8. Question 8 of 12.

    This has_path_sum has one change from the lesson's: it checks remaining == 0 at every node, not only at a leaf. The tree is [2, 3, 6, None, 1]: 2 on top with children 3 and 6, and 1 as the right child of 3. Type exactly what it prints.

    def has_path_sum(root, target):
        if root is None:
            return False
        remaining = target - root.val
        if remaining == 0:
            return True
        return has_path_sum(root.left, remaining) or has_path_sum(root.right, remaining)
    
    tree = build_tree([2, 3, 6, None, 1])
    print(has_path_sum(tree, 5), has_path_sum(tree, 6), has_path_sum(tree, 2))
    
  9. Question 9 of 12.

    A tree is a chain of 50,000 nodes, each the left child of the one above. In this lab you run two versions of max depth on it: the recursive one, 1 + max(max_depth(node.left), max_depth(node.right)) with 0 for None, and count_levels, the queue version that counts rounds. What happens?

  10. Question 10 of 12.

    Here is build_tree([1, 2, 3, 4, None, 5, None, 6, None, 7]):

            1
          /   \
         2     3
        /     /
       4     5
      /     /
     6     7
    

    A tree is balanced when, at every node, the heights of the left and right subtrees differ by at most 1. Which statement is true?

  11. Question 11 of 12.

    This is split_point from the BST lesson:

    def split_point(root, a, b):
        node = root
        while True:
            if a < node.val and b < node.val:
                node = node.left
            elif a > node.val and b > node.val:
                node = node.right
            else:
                return node.val

    The tree is the BST build_tree([20, 10, 30, 5, 15, 25, 35, None, None, 12]): 20 on top, 10 and 30 below it, 5 and 15 under 10, 25 and 35 under 30, and 12 as the left child of 15. What does split_point(root, 12, 15) return? Type the value.

    value

    Type a number: 12, -1, 0.5 and 3/4 all work.

  12. Question 12 of 12.

    Run the tests before you submit. This question counts as correct if your latest run passes every check.

    Code itSum of the left leaves

    A left leaf is a leaf (a node with no children) that is the left child of its parent. Write sum_left_leaves(root), which returns the sum of the values of all left leaves, and 0 when there are none.

    In [4, 2, 7, 1, 3, 6], the leaves are 1, 3 and 6. 1 is the left child of 2 and 6 is the left child of 7, but 3 is a right child, so the answer is 1 + 6 = 7. A tree of one node has no left leaf: the root is a leaf, but it is nobody's child. Then press Watch it run.

    ⌘+Enter runsPython starts when a code cell comes near or you run one
    Write your code where the starter says raise NotImplementedError, then press Run tests. Each check says what it expects.
0 of 12 answered. Unanswered questions count as wrong.