Question: Complete the sum_of_leaves() function which takes a single parameter, binary_tree, a BinaryTree object, and returns the sum of its leaves. The function has to be

Complete the sum_of_leaves() function which takes a single parameter, binary_tree, a BinaryTree object, and returns the sum of its leaves. The function has to be a recursive function. No loops are allowed. You can find the code for the Binary Tree class here. This code has been provided as part of the question setup and you must not include it in your solution. You can assume that BinaryTree objects will only contain integer data. Some examples of the function being used are shown below. For example: Test Result Sum of leaves: 10 binary_tree = Binary Tree (10) print("Sum of leaves:", sum_of_leaves(binary_tree)) Sum of leaves: 47 binary_tree = BinaryTree(5) binary_tree.set_left(BinaryTree(12)) binary_tree.set_right(BinaryTree (35)) print("Sum of leaves:", sum_of_leaves (binary_tree)) Sum of leaves: 77 binary_treel = BinaryTree(5) binary_tree2 = Binary Tree (12) binary_tree3 = BinaryTree (19) binary_tree4 = BinaryTree (26) binary_tree5 = BinaryTree (51) binary_treel.set_left(binary_tree2) binary_treel.set_right(binary_tree3) binary_tree2.set_left(binary_tree4) binary_tree3.set_left(binary_tree5) print("Sum of leaves:", sum_of_leaves (binary_tree1)) Answer: (penalty regime: 0, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 %) Reset answer 1 - def sum_of_leaves (binary_tree):

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!