Kotlinlearncs.online LogoJava
Return to List

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.

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-size/[email protected]