Kotlinlearncs.online LogoJava
Return to List

Homework: BinaryTree to Map

Created By: Geoffrey Challen
/ Version: 2021.4.0

Create a method toMap that accepts a BinaryTree<*> and returns a Map<Any, Int> mapping the values in the tree to the count of the times that the value appears.

Our suggestion is to have toMap create the map and then call a private recursive helper method to populate it. You will need to import cs125.trees.BinaryTree. We've provided some code to get you started.

For reference, cs125.trees.BinaryTree is defined like this:

Note that you may need to cast tree.value to Any so that you can add it to your map.

Related Lessons

Stuck? You may find these lessons helpful: