Tell us what’s happening:
There is problem in test case. My code is passed the test case, but actually on my system I chacked with tree example, it throw an error. So, I think there is problem in test cases.
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.findMaxHeight=function(){
if(this.root===null){
return -1;
}
else{
let ans=findMax(this.root);
return ans-1; //add root also
}
}
function findMax(tree){
let lans=0;
let rans=0;
if(tree.left!==null){
lans=1;
lans+=findMax(tree.left);
}
else if(tree.right!==null){
rans=1;
rans+=findMax(tree.left);
}
else{
return 1;
}
if(lans>rans){
return lans;
}
else{
return rans;
}
}
this.findMinHeight=function(){
//if tree is empty
if(this.root===null){
return -1;
}
else{
let ans=findMin(this.root);
return ans+1;
}
}
function findMin(tree){
let lans=0;
let rans=0;
if(tree.left!==null){
lans+=findMin(tree.left);
}
else if(tree.right!==null){
rans+=findMin(tree.left);
}
else{
return 1;
}
if(lans<rans){
return lans;
}
else{
return rans;
}
}
// change code above this line
}
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36
.
Challenge: Find the Minimum and Maximum Height of a Binary Search Tree
Link to the challenge:
https://www.freecodecamp.org/learn/coding-interview-prep/data-structures/find-the-minimum-and-maximum-height-of-a-binary-search-tree