Kotlinlearncs.online LogoJava
Return to List

Solve: Binary Tree Combine Even Strings

Created By: Pranav Narayanan
/ Version: 2025.2.0

Write a method named combineEvenStrings that accepts a single BinaryTree<String> and returns the concatenation of all even length Strings in this tree, in the order current + left + right (post-order traversal).

For example, given the tree:

      "12"
     /    \
  "cs"    "4"

You would return "12cs", by concatenating the current node "12", the left subtree "cs", and the right subtree "" (the right subtree has no even length Strings). If the tree was:

      "24"
     /
  "1"
     \
      "cs"

You would return "24cs", by concatenating the current node "24", and the left subtree "cs" (you ignore "1" because it is not even length). You may need to write a private helper method to solve this problem.

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

Related Lessons

Stuck? You may find these lessons helpful:

Report a Problem

Attribution must link to this page: https://www.learncs.online/solve/kotlin/binary-tree-combine-even-strings/[email protected]