Question about the prompt on "Data Structures: Find the Minimum and Maximum Height of a Binary Search Tree"

Hey there, I was working on the data structures part of FCC, and got to the part about finding maxHeight and minHeight of a Binary Search Tree (https://learn.freecodecamp.org/coding-interview-prep/data-structures/find-the-minimum-and-maximum-height-of-a-binary-search-tree).

When independently testing, I find that my code does return a number. But when testing with FCC, it returns saying none of my methods pass the test. I’m not sure if I’m misinterpreting what FCC is asking for, or maybe I don’t understand what they really mean with maxHeight/minHeight.

My code is below, and any input is helpful.

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.findMaxHeight = function(){

    	function recurPath(node){
    		if(node.left == null && node.right == null){
    			return 1;
    		}
    		else if(node.left == null){
    			return(1 + recurPath(node.right));
    		}
    		else if(node.right == null){
    			return(1 + recurPath(node.left));
    		}
    		else{
    			let lPath = recurPath(node.left);
    			let rPath = recurPath(node.right);

    			if(lPath > rPath){
    				return(1 + lPath);
    			}
    			else{
    				return(1 + rPath);
    			}
    		}
    	}

    	if(this.root == null){
    		return (-1);
    	}
    	else{
    		return recurPath(this.root);
    	}
    }

    this.findMinHeight = function(){
    	function recurPath(node){
    		if(node.left == null && node.right == null){
    			return 1;
    		}
    		else if(node.left == null){
    			return(1 + recurPath(node.right));
    		}
    		else if(node.right == null){
    			return(1 + recurPath(node.left));
    		}
    		else{
    			let lPath = recurPath(node.left);
    			let rPath = recurPath(node.right);

    			if(lPath < rPath){
    				return(1 + lPath);
    			}
    			else{
    				return(1 + rPath);
    			}
    		}
    	}

		if(this.root == null){
    		return (-1);
    	}
    	else{
    		return recurPath(this.root);
    	}    	
    }

    this.isBalanced = function(){
    	let maxHt = this.findMaxHeight();
    	let minHt = this.findMinHeight();

    	if((maxHt - minHt) > 1){
    		return false;
    	}
    	else{
    		return true;
    	}
    }
    // change code above this line
}

This is double counting your leaf nodes. Since you’re always adding 1 to the return value of your function, the null check should be 0.

Hey there, so this worked, but I’m confused about what height means now. Does that mean a tree with a single node has a height of 0?

Yup. The challenge doesn’t say so explicitly, but think about the definition it gives:

Height in a tree represents the distance from the root node to any given leaf node.

What is the distance of a root node to a non-existent leaf? The Wikipedia article is more blunt:

The root node has depth zero, leaf nodes have height zero, and a tree with only a single node (hence both a root and leaf) has depth and height zero.

Ah I totally see where I went wrong now. Thanks for enlightening me! But my .isBalance() method still doesn’t work right either. Am I misinterpreting the meaning of ‘balanced’ as well??

NVM. Stand by while I reassess my thoughts.

I think you understand the concept and your code is correct. This could just be because I haven’t had any coffee yet, but it seems that the logic of the challenge is backwards.

Sure thing, really appreciate all the help so far! If you happen to find out where I went wrong, feel free to comment again.