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

Trees and Recursion

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

Next we’ll continue practicing with trees and recursion! And what better way to do that than to do a few problems together? So let’s get started!

Count Left Greater Than Right
Count Left Greater Than Right

As a warm up, let’s do another counting problem. Given a binary tree containing Integers, let’s count the number of nodes where the value of the left child is greater than the value of the right child.

Before we start, remember the core of our approach to recursion:

// Binary Tree Count Left Greater Than Right

Tree Search
Tree Search

Next, we’ll look at how to determine if a binary tree contains a certain value. This problem introduces a new wrinkle to our usual approach to recursion!

// Binary Tree Search

Practice: Solve Binary Tree Count Equal to Child

Created By: Geoffrey Challen
/ Version: 2026.11.0

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:

Algorithm Analysis
Algorithm Analysis

Next, let’s examine the performance of our recursive algorithms, and determine what O(n) category they belong in.

// Binary Tree Algorithm Analysis

The shape of a tree matters too. Some algorithms only walk down one path from the root, such as following left children until there are none. In a tree where every node has only one child, which is what you get if new nodes are always added on the same side, that path includes every node. A tree like that is really a linked list, and walking it is O(n), just like walking a linked list.

Homework: Solve BinaryTree Count Equal Children

Created By: Geoffrey Challen
/ Version: 2026.11.0

Create a public class BinaryTreeCounter that provides a single class method named countEqualChildren that accepts a single BinaryTree<?> and counts the number of nodes in the tree that have two children with equal values. 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 the values in the tree are non-null.

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

More Practice

Need more practice? Head over to the practice page.