Skip to main content
Recursion and trees 0 of 6 activities solved
  1. Recursion and Trees Not yet read
  2. Recursion and Trees: quiz Not yet read
  3. Define Data Not yet solved
  4. Debug Tree Not yet solved
  5. Fill Tree Not yet solved
  6. Evaluate Operator Not yet solved
Next series Dictionaries
Recursion and trees

Debug Tree

Sign in to hand in your solution and get feedback.

from dataclasses import dataclass from bakery import assert_equal @dataclass class Tree: pass @dataclass class BinaryTree: value: int left: ___ right: Tree EMPTY = BinaryTree() def add_up_tree(tree: Tree) -> int: if tree == EMPTY: return 0 left_side = tree.left right_side = add_up_tree(tree.right) combined = left_side + right_side return 'value' + combined assert_equal(add_up_tree(BinaryTree(7, EMPTY, EMPTY)), 7) assert_equal(add_up_tree(BinaryTree(7, BinaryTree(3, EMPTY, EMPTY), EMPTY)), 10) assert_equal(add_up_tree(BinaryTree(7, BinaryTree(3, BinaryTree(1, EMPTY, EMPTY), EMPTY), BinaryTree(9, EMPTY, EMPTY))), 20) assert_equal(add_up_tree(EMPTY), 0)