Data Structures - Find the Minimum and Maximum Height of a Binary Search Tree

Tell us what’s happening:
Describe your issue in detail here.

Please somebody help, I am not able to pass the test, In my opnion I am getting the expected result, I don’t know where I am going wrong or which sample test data I am failing. Is there anyway that I can find freeCodeCamp sample test data and I can test on those data set because I had to come to this forum to ask similar kind of question.

Your code so far

var displayTree = tree => console.log(JSON.stringify(tree, null, 2));
function Node(value) {
  this.value = value;
  this.left = null;
  this.right = null;
}
function BinarySearchTree() {
  this.root = null;
  // Only change code below this line

    this.add = function(ele){
    let node = new Node(ele);
    if(!this.root){
      this.root = node;
    }
    else {
      this.insert(this.root,node)
    }
  
  };
  this.insert = function(root,newNode){
      if(root==newNode){
        return null;
      }
      else if(newNode.value < root.value){
        if(!root.left) {
         root.left = newNode;
        } else {
          this.insert(root.left,newNode);
        }
     }
     else if(newNode.value > root.value){
       if(!root.right) {
         root.right = newNode;
       } else {
         this.insert(root.right,newNode);
       }
     }
  }

  this.findMaxHeight = function(root) {
    if(!root){
      return -1
    }
    
    return Math.max(this.findMaxHeight(root.left)+1,
                      this.findMaxHeight(root.right)+1)
  
  }
  
  this.findMinHeight = function(root) {
    if(!root){
      return -1
    }
    
    return Math.min(this.findMinHeight(root.left)+1,
                      this.findMinHeight(root.right)+1)
  
  }

  this.isBalanced = function (root) {
    if(this.findMaxHeight(root) - this.findMinHeight(root)<=1){
      return true;
    }else {
      return false;
    }
  }
  // Only change code above this line
}

let bst = new BinarySearchTree();
bst.add(50);
bst.add(40);
bst.add(60);

console.log(bst.findMaxHeight(bst.root));
console.log(bst.findMinHeight(bst.root));
console.log(bst.isBalanced(bst.root))

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36

Challenge: Data Structures - Find the Minimum and Maximum Height of a Binary Search Tree

Link to the challenge:

Results are good, but with some changes in how methods are used. They are not expected to take any argument. One of the tests is using the following numbers:

const bst = new BinarySearchTree();
bst.add(4);
bst.add(1);
bst.add(7);
bst.add(87);
bst.add(34);
bst.add(45);
bst.add(73);
bst.add(8);
console.log(bst.findMinHeight()) // 1
console.log(bst.findMaxHeight()) // 5
console.log(bst.isBalanced()) // false
1 Like

Thanks sanity, its so so stupidity… why I am not able to figure out by my own. I took lots of time to get it and pass the test even after reading your hint.

I just did function(root=this.root)

This is for future reader, those who don’t have to waste time if they get the correct answer and I can understand the constraint with which freeCodeCamp have to design and work.

1 Like

That’s how the mind works. When focused on little details, it tends to overlook a more general aspects. The other way around it also applies.

That’s pretty nice way to handle the problem. I had a way more elaborate solution for this in my mind :eyes:

1 Like

thanks for the encouraging words…