KotlinCS 124 LogoJava
PrevIndexNext
Kotlin
Java
  • Practice with Tree Recursion : 11/09/2026

  • Trees and Recursion : 11/06/2026

  • Trees : 11/05/2026

  • Recursion : 11/04/2026

  • Working With Agents: Say What You Saw : 11/03/2026

  • Working With Agents: Validate What It Built : 11/02/2026

  • Lists Review and Performance : 10/30/2026

  • Linked Lists : 10/29/2026

  • Algorithms and Lists : 10/28/2026

  • Working With Agents: Correct It, Out Loud, Early : 10/27/2026

  • Working With Agents: Discuss Ideas, Not Implementations : 10/26/2026

  • Lambda Expressions : 10/23/2026

  • Anonymous Classes : 10/22/2026

  • Practice with Interfaces : 10/21/2026

  • Implementing Interfaces : 10/20/2026

  • Using Interfaces : 10/19/2026

  • Working with Exceptions : 10/16/2026

  • Throwing Exceptions : 10/15/2026

  • Catching Exceptions : 10/14/2026

  • References and Polymorphism : 10/13/2026

  • References : 10/12/2026

  • Data Modeling 2 : 10/09/2026

  • Equality and Object Copying : 10/08/2026

  • Polymorphism : 10/07/2026

  • Inheritance : 10/06/2026

  • Data Modeling 1 : 10/05/2026

  • Static : 10/02/2026

  • Encapsulation : 10/01/2026

  • Constructors : 09/30/2026

  • Objects, Continued : 09/29/2026

  • Introduction to Objects : 09/28/2026

  • Testing and Compilation : 09/25/2026

  • Practice with Collections : 09/24/2026

  • Maps and Sets : 09/23/2026

  • Lists and Type Parameters : 09/22/2026

  • Imports and Libraries : 09/21/2026

  • Multidimensional Arrays : 09/18/2026

  • Practice with Strings : 09/17/2026

  • null : 09/16/2026

  • Algorithms and Strings : 09/15/2026

  • Strings : 09/14/2026

  • Functions and Algorithms : 09/11/2026

  • Practice with Functions : 09/10/2026

  • More About Functions : 09/09/2026

  • Errors and Debugging : 09/08/2026

  • Functions : 09/04/2026

  • Practice with Loops and Algorithms : 09/03/2026

  • Algorithms : 09/02/2026

  • Loops : 09/01/2026

  • Arrays : 08/31/2026

  • Compound Conditionals : 08/28/2026

  • Conditional Expressions and Statements : 08/27/2026

  • Operations on Variables : 08/26/2026

  • Variables and Types : 08/25/2026

  • Welcome to CS 124 : 08/24/2026

Practice with Tree Recursion

import cs125.trees.BinaryTree;
int treeDepth(BinaryTree tree) {
return 0;
}
assert treeDepth(new BinaryTree<Integer>(0, 1, 2)) == 1;

Guess what? In this lesson we’ll be doing more practice with binary trees! And recursion! What could be more fun?

Warm Up Debugging Challenge
Warm Up Debugging Challenge

But, would we want to miss out on another debugging challenge? No way!

Create a public class BinaryTreeCounter that provides a single class method named countEqualToEitherChild that accepts a single BinaryTree and counts the number of nodes in the tree where the value at that node is equal to either the value at its right child or the value at its left child. Keep in mind that not every node has a right or left child, so you'll need to check for null carefully. (Or use try-catch!) However, you can assume that all of the values in the tree are non-null.

Your solution must be recursive: countEqualToEitherChild should call itself on the left and right subtrees, or call a private recursive helper method that does. A loop that walks the tree won't pass. For reference, cs125.trees.BinaryTree has the following public properties:

Instructions

Tree Depth
Tree Depth

As a warm up let’s write a recursive function to determine the depth or height of a tree. As a reminder, the depth is defined as the distance from the root node to the farthest leaf node. (The depth is not defined for an empty tree, since it has no root.)

// Tree Depth

Tree Node Count
Tree Node Count

Next, let’s look at an example of a recursive function that passes another data structure around. We’ll write a recursive method that returns an array with counts of the number of nodes that have zero, one, or two children. This will also prepare you for this lesson’s homework problem—which is a tricky one!

// Tree Child Count

Practice: Solve Binary Tree Sum Even Depths

Created By: Geoffrey Challen
/ Version: 2026.11.0

Write a method sumEvenDepths that accepts a BinaryTree<Integer> and returns an int: the sum of the values in all of the nodes that are at an even depth in the tree. The tree passed to sumEvenDepths will not be null.

The root node is at depth 0, its children are at depth 1, their children are at depth 2, and so on. So the values that you should add up are the root's value, the values of its grandchildren, the values of their grandchildren, and so on.

Our suggestion is to have sumEvenDepths call a private recursive helper method that accepts both a tree and the depth of that tree's root node. Then each recursive call can pass its children a depth that is one larger.

Your solution must be recursive: sumEvenDepths should call itself on the left and right subtrees, or call a private recursive helper method that does. A loop that walks the tree won't pass. For reference, cs125.trees.BinaryTree has the following public methods:

Homework: Solve BinaryTree to Map

Created By: Geoffrey Challen
/ Version: 2026.11.0

Create a public class BinaryTreeToMap that provides a single static method toMap. toMap accepts a BinaryTree<?> and returns a Map<Object, Integer> 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. If the tree passed to toMap is null you should throw an IllegalArgumentException. You will need to import cs125.trees.BinaryTree, as well as Map and a Map implementation (probably HashMap) from java.util. We've provided some code to get you started.

Your solution must be recursive: toMap should call itself on the left and right subtrees, or call a private recursive helper method that does. A loop that walks the tree won't pass. For reference, cs125.trees.BinaryTree has the following public properties:

import cs125.trees.BinaryTree;
import java.util.Map;
public class BinaryTreeToMap {
public static Map<Object, Integer> toMap(BinaryTree<?> tree) {
// Check for null
// Create your map
// Call the helper method to populate the map
// Return the map
return null;
}
// Helper method
private static void toMap(BinaryTree<?> tree, Map<Object, Integer> values) {}
}

More Practice

Need more practice? Head over to the practice page.