Find the Minimum and Maximum Height of a Binary Search Tree

Find the Minimum and Maximum Height of a Binary Search Tree
0

#1

Tell us what’s happening:
this.isBalanced method is not working.Help!

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;
    // change code below this line
    this.findMinHeight = function(root=this.root) {
        if(root===null){
            return -1;
        }
        else if(root.left===null && root.right===null){
            return 0;
        }
        else if(root.left==null){
            return (this.findMinHeight(root.right)+1);
        }else if(root.right==null){
            return (this.findMinHeight(root.left)+1);
        }else{
        return Math.min(this.findMinHeight(root.left),this.findMinHeight(root.right)) + 1;
        }
    }

    this.findMaxHeight = function(root=this.root) {
        if(root===null){
            return -1;
        }
        else if(root.left===null && root.right===null){
            return 0;
        }
        else if(root.left==null){
            return (this.findMaxHeight(root.right)+1);
        }else if(root.right==null){
            return (this.findMaxHeight(root.left)+1);
        }else{
        return Math.max(this.findMaxHeight(root.left),this.findMaxHeight(root.right)) + 1;
        }
    }


    this.isBalanced = function(root=this.root){
        if(root===null || root.left===null || root.right===null){
            return true;
        }
        let l = this.findMaxHeight(root.left);
        let r = this.findMaxHeight(root.right);
        let difference = Math.abs(r - l);    
        if(difference <= 1 && this.isBalanced(root.left) && this.isBalanced(root.right))
            {
               return true;
            }
        return false;
    }
    // change code above this line
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/coding-interview-prep/data-structures/find-the-minimum-and-maximum-height-of-a-binary-search-tree