beyondgrader.com Logo
DemoBrowseAboutTeamLogin

BinaryTree Balanced

Geoffrey Challen // 2020.11.0

Let's determine if a binary tree is height balanced. A tree is height balanced if the height of any node's two subtrees (right and left) never differ by more than 1.

Provide a public class named BinaryTreeBalanced providing a single public class method named isBalanced. isBalanced accepts a BinaryTree and returns true if the tree is balanced, and false otherwise. If the passed tree is null, you should throw an IllegalArgumentException.

A few hints on this problem:

  • Your main entry point method will probably need to start the recursion using a private helper method, because the main method needs to throw on null which you want to handle as a valid base case in your recursion
  • This helper method will probably have the same arguments as the main method, so you'll need to change something else to make sure that the method signature is different and the compiler can determine which one you want to use
  • You will probably need a second helper method implementing tree height that you call in your primary recursive method