Java Challenge - Balanced Binary Search Tree

This community-built FAQ covers the “Balanced Binary Search Tree” code challenge in Java. You can find that challenge here, or pick any challenge you like from our list.

Top Discussions on the Java challenge Balanced Binary Search Tree

There are currently no frequently asked questions or top answers associated with this challenge – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this challenge. Ask a question or post a solution by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this challenge, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I had some trouble getting this challenge to run (and I had to create an internal TreeNode class to avoid errors). TreeNode has a constructor that takes in an Integer and makes it the .value
I used array slicing this time, and I made a function to do the slicing.

Here’s my code:

import java.lang.*;
import java.util.Arrays;

public class BalancedBST {

  public static Integer[] slice(Integer[] arr, int start, int afterEnd) {
    int length = afterEnd - start;
    Integer[] part = new Integer[length];
    int j = 0;
    for (int i = start; i < afterEnd; i++) {
      part[j] = arr[i];
      j++;
    }
    return part;
  }

  public static TreeNode balancedBst(Integer[] a) {
    if ((a == null) || (a.length < 1)) {
      return null;
    }
    else if (a.length == 1) {
      return new TreeNode(a[0]);
    }
    else {
      int mid = a.length / 2;  // integer division
      TreeNode node = new TreeNode(a[mid]);
      node.left = balancedBst(slice(a, 0, mid));
      node.right = balancedBst(slice(a, mid + 1, a.length));
      return node;
    }
  }
    
  public static void main(String[] args){
    Integer[] a = {1,2,3,4,5,6,7,8};
    TreeNode balancedTree = balancedBst(a);
    balancedTree.print();
  }
  /*
  public static class TreeNode {
    public Integer value;
    public TreeNode left; // initialized to null
    public TreeNode right; // initialized to null

    public TreeNode() {
      this.value = null;
    }
    public TreeNode(Integer value) {
      this.value = value;
    }
    public void print() {
      System.out.print(this.value);
      if (!(this.left == null && this.right == null)) {
        System.out.print(" -> ");
        if (this.left != null) {
          System.out.print(this.left.value);
          if (this.right != null) {
            System.out.print(",");
          }
        }
        if (this.right != null) {
          System.out.print(this.right.value);
        }
        System.out.println();
        if (this.left != null) {
          this.left.print();
        }
        if (this.right != null) {
          this.right.print();
        }
      }
      else {
        System.out.println();
      }
    }
  }
  */
}
version without slicing
  public static TreeNode balancedBst(Integer[] a, int start, int afterEnd) {
    if ((a == null) || start >= afterEnd) {
      return null;
    }
    else if ((start + 1) == afterEnd) {
      return new TreeNode(a[start]);
    }
    else {
      int mid = (start + afterEnd) / 2;  // integer division
      TreeNode node = new TreeNode(a[mid]);
      node.left = balancedBst(a, start, mid);
      node.right = balancedBst(a, mid + 1, afterEnd);
      return node;
    }
  }
  public static TreeNode balancedBst(Integer[] a) {
    return balancedBst(a, 0, a.length);
  }

I had the same issue.
The code would only run if I created a static TreeNode class inside the other class,
but I had to delete that when I submitted the challenge.

internal TreeNode class (taken from JavaScript version)
public static class TreeNode {
  public Integer value;
  public TreeNode left;
  public TreeNode right;

  public TreeNode(value) { //constructor
    this.value = value;
    this.left = null;
    this.right = null;
  }

  print() {
    // outputs node values going down
    // the left side of the tree first
    // then working right
    if (this.left != null) {
      System.out.println(this.value + " -> " + this.left.value);
      this.left.print();
    }

    if (this.right != null) {
      System.out.println(this.value + " -> " + this.right.value);
      this.right.print();
    }
  }
}

I guess I forgot to write the type for the constructor

  public TreeNode(Integer value) { //constructor
    this.value = value;
    this.left = null;
    this.right = null;
  }