Tell us what’s happening:
Tell me what is wrong.
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.isPresent=function(element){
if(this.root===null){
return null;
}
else{
let pre=this.root;
let ans;
while(true){
if(pre.value===element){
ans=true;
break;
}
else if(pre.value<element){
if(pre.right!==null){
pre=pre.right;
}
else{ans=false;break;}
}
else{
if(pre.left!==null){
pre=pre.left;
}
else{ans=false;break;}
}
}
return ans;
}
}
// 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: Check if an Element is Present in a Binary Search Tree
Link to the challenge:
https://www.freecodecamp.org/learn/coding-interview-prep/data-structures/check-if-an-element-is-present-in-a-binary-search-tree