Check your understanding of the previous reading: tree vocabulary, the parts of a recursive function, recursion and loops, and the types of the expressions in a recursive function.
Match each term to the best definition.
Match each term from the reading to the best explanation.
Given the following code, determine the most accurate type for each expression on its line.
from dataclasses import dataclass
@dataclass
class Tree:
pass
@dataclass
class BinaryTree(Tree):
value: int
left: Tree
right: Tree
EMPTY = Tree()
def sum_tree(tree: Tree) -> int:
if tree == EMPTY:
return 0
left_side = sum_tree(tree.left)
right_side = sum_tree(tree.right)
result = tree.value + left_side + right_side
return result