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
}