Kotlinlearncs.online LogoJava
Return to List

Solve: BinaryTree to List

Created By: Geoffrey Challen
/ Version: 2020.11.0

Create method toList that accepts a BinaryTree<Any> and returns a List<Any> containing all of the values in the tree, in any order.

Our suggestion is to have toList create the list 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:

import cs125.trees.BinaryTree
fun toList(tree: BinaryTree<*>): List<Any>? {
// Create the list
// Populate it using the helper method
// Return the list
return null
}
// Helper method
private fun toList(
tree: BinaryTree<*>?,
values: MutableList<Any>,
) {
}

Related Lessons

Stuck? You may find these lessons helpful: