Kotlinlearncs.online LogoJava
Return to List

Solve: Binary Tree Combine Even Strings

Created By: Pranav Narayanan
/ Version: 2025.2.0

Create a public class TreeCombineStrings that provides a single class method named combineEvenStrings. combineEvenStrings accepts a single BinaryTree<String>. If the passed tree is null, throw an IllegalArgumentException. Otherwise, return 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 has the following public properties:

Related Lessons

Stuck? You may find these lessons helpful:

Report a Problem

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