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:
Identify the base case—the simplest problem that you have to be able to immediately solve
Make the problem smaller at each step
Combine results appropriately
Content Restricted to Current CS 124 Students
A publicly-accessible version of this content is available at learncs.online.
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!
Content Restricted to Current CS 124 Students
A publicly-accessible version of this content is available at learncs.online.
// Binary Tree Search
Problem Restricted to Current CS 124 Students
A publicly-accessible version of this content is available at learncs.online.
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:
Next, let’s examine the performance of our recursive algorithms, and determine what O(n) category they belong in.
Content Restricted to Current CS 124 Students
A publicly-accessible version of this content is available at learncs.online.
// 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.
Problem Restricted to Current CS 124 Students
A publicly-accessible version of this content is available at learncs.online.
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: