Embercode
✕
Binary TreesQuick quiz

Iterative traversals

8 questions. Covers Iterative postorder with one stack in Python.

Questions 1 to 5 of 8

  1. 1. The preorder function from this lesson runs on this tree. What does it print? ``python root = TreeNode(8) root.left = TreeNode(3) root.right = TreeNode(10) root.left.left = TreeNode(1) root.left.right = TreeNode(6) root.right.right = TreeNode(14) print(preorder(root)) ``
  2. 2. In an empty list used as a stack, what does stack[-1] do?
  3. 3. In the lesson's postorder function, what are the steps for one round of the outer loop, in order?

    Use the arrows to put the steps in order.

    1. 1.If its right child exists and is not last_visited, set current to that right child and go round again
    2. 2.Otherwise pop the top node, visit it, and set last_visited to it
    3. 3.Peek at the top node with stack[-1] without removing it
    4. 4.Walk left from current, pushing every node, until current is None
  4. 4. What does this print, using a list as the stack from this lesson? ``python stack = [] stack.append(4) stack.append(7) stack.append(9) a = stack.pop() stack.append(2) print(a, stack[-1], stack) ``
  5. 5. In the lesson's preorder trace for the sample tree, what are the first five steps, in order?

    Use the arrows to put the steps in order.

    1. 1.Pop 1, visit it and add it to the result
    2. 2.Push the left child 2 on top of it
    3. 3.Pop 2, visit it and add it to the result
    4. 4.Push the root 1 onto the stack
    5. 5.Push the right child 3

3 question(s) left on this page.