KotlinCS 124 LogoJava
PrevIndexNext
Kotlin
Java
  • 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

Trees

public class BinaryTree {
private Object value;
private BinaryTree left;
private BinaryTree right;
public BinaryTree(Object setValue) {
value = setValue;
}
public Object getValue() {
return value;
}
public BinaryTree getLeft() {
return left;
}
public BinaryTree getRight() {

This lesson introduces a new data structure: trees. Trees are extremely useful, both for modeling certain types of data, and for enabling certain algorithms. They are also great for practice with recursion! Let’s do this…

Warm Up Debugging Challenge
Warm Up Debugging Challenge

But… you knew it! Let’s warm up with another graded debugging challenge!

Create a public class Factorial that provides a single static method factorial. factorial accepts a single long and returns its factorial as a long. You can reject negative arguments and ones greater than 20 by throwing an IllegalArgumentException.

You should submit a recursive solution. The factorial of 0 is 1, and this represents the base case. The factorial of n is n * the factorial of n - 1, and this represents the recursive step.

Instructions

What is a Tree?
What is a Tree?

Wikipedia defines a (computer science) tree as:

In computer science, a tree is a widely used abstract data type that simulates a hierarchical tree structure, with a root value and subtrees of children with a parent node, represented as a set of linked nodes.

Let’s look at some diagrams that will help visualize this new data structure, and introduce some important terminology.

What are Trees For?
What are Trees For?

Trees can be used to model a variety of hierarchical data. Examples include:

Let’s look at these example a bit more:

We’ll also see ways that trees can be used to store a collection of items in ways that enable certain efficient algorithms. For example, by storing data in a tree, we can enable more efficient search algorithms than the O(n) scans of arrays we’ve seen so far. We’ll get there!

Binary Trees
Binary Trees

Most of the work with trees that we’ll be doing is on a specific type of tree called a binary tree. Binary trees are so named because each node has up to two children, but no more.

Let’s develop the BinaryTree class that we’ll use in class and that we’ll use on upcoming homework problems. Note that this is available in the playground environments and homework as cs125.trees.BinaryTree. As a way of reviewing object design and references, let’s walk through the process of designing this class:

// BinaryTree class

Recursion on Trees
Recursion on Trees

In the previous lesson we introduced recursion, a problem solving technique that works by breaking down large problems into smaller pieces, solving them, and then combining the results. Binary trees are a great fit for recursion! Let’s see why:

In addition, compared to lists and arrays and Strings, trees are hard to work with using iterative solutions! Recursion is a much better approach…

Recursive Tree Sum
Recursive Tree Sum

Let’s get some practice with recursion on trees. We’ll take a tree filled with integer values and determine the sum of all the values it stores. Along the way, we’ll look at several recursive approaches, and begin to develop some of the patterns that we’ll use working recursively with binary trees.

// BinaryTree Sum

Recursive Tree Size
Recursive Tree Size

To help you prepare for our next homework problem, let’s discuss the recursive approach to counting the number of nodes in a tree.

Notice how often the base case runs. A method that starts by checking for null reaches its base case once for every missing child, and a binary tree with n nodes always has n + 1 of those. So on a tree with three nodes, the method reaches its base case four times, whatever shape the tree has.

Practice: Solve BinaryTree Negative Sum

Created By: Geoffrey Challen
/ Version: 2026.11.0

Create a public class named BinaryTreeNegativeSum with a single class method named negativeSum. negativeSum accepts a BinaryTree<Integer>, that is a BinaryTree containing Integer values. Return the sum of all the negative values in the tree.

Your solution must be recursive: negativeSum 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:

Computational Complexity
Computational Complexity

Let’s pause for a moment to discuss how we evaluate the complexity of your homework submissions, and what you can do to improve your code when we indicate that it is too complicated. First, a practice problem to work on. Once you’ve solved this, the solution walkthrough will show what happens to an overly-complex submission, and discuss how to reduce the complexity of your code.

The practice problem below looks like a regular practice problem, but it introduces a new way in which we’ll begin scoring your homework solutions: evaluating their complexity. Starting in this lesson there is 1 point out of 10 you’ll earn for not submitting an overly-complex solution. Solve the problem and then watch a solution walkthrough to learn more!

Created By: Geoffrey Challen
/ Version: 2021.7.0

Declare and implement a function called sumIsOdd. sumIsOdd should accept two int arguments and return true if their sum is odd and false otherwise. You will probably want to consider using the remainder operator (%) to complete this problem.

Homework: Solve Binary Tree Size

Created By: Geoffrey Challen
/ Version: 2026.11.1

Create a public class BinaryTreeSize that provides a single static method size. size accepts a BinaryTree<?>, a BinaryTree that can contain any value, and returns the number of nodes it contains. You'll want to count recursively, identifying both a base case and a recursive step.

For reference, BinaryTree has the following public properties:

Don't overthink this! Like many recursive algorithms, the solution is elegant and simple: 4 lines total if you do it right. BinaryTree comes from cs125.trees.BinaryTree, so you'll need to add import cs125.trees.BinaryTree; to the top of your file, both here and in similar problems.

CS People: Roy Clay Sr.
CS People: Roy Clay Sr.

Roy Clay Sr. (1929-2024) was known as the “Godfather of Silicon Valley” for his pioneering work in computing and his efforts to open doors for other African Americans in technology. Born in Kinloch, Missouri, he became the first African American to graduate from St. Louis University in 1951—when computer science didn’t yet exist as a field.

Personally recruited by Dave Packard in 1965, Clay founded and led Hewlett-Packard’s computer division. He directed the team that developed the HP 2116A, one of HP’s first minicomputers, and wrote the software for it himself. His technical leadership during the formative years of Silicon Valley helped establish HP as a major force in computing.

Beyond his technical achievements, Clay used his position as HP’s highest-ranking African American employee to combat discrimination and create pathways for others. He established hiring programs targeting HBCUs and mentored countless engineers. He later served on Palo Alto’s city council and was inducted into the Silicon Valley Hall of Fame in 2003.

More Practice

Need more practice? Head over to the practice page.