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: