beyondgrader.com Logo
DemoBrowseAboutTeamLogin

BinaryTree Same Shape

Geoffrey Challen // 2020.11.0

Create a public class named BinaryTreeSameShape with a static method sameShape that accepts two BinaryTree arguments and returns true if they have the same shape and false otherwise. Two binary trees have the same shape if they have the same nodes in the same positions, regardless of the values that each node contains.

sameShape should throw an IllegalArgumentException if either of its arguments are null. As a result, you probably want to set up a private helper function to actually perform the recursion, allowing you to handle null as a base case.

In this problem you'll perform recursion on both trees at the same time. But in every other way it's a typical tree recursion problem. Consider your base case: if you reach a null subtree for both trees in the same step, then they have the same shape at that point in the tree. Otherwise consider the other cases and how to handle them, as well as how to continue the recursion and combine the results from your recursive steps together.

For reference, cs125.trees.BinaryTree has the following public properties: