freeCodeCamp Challenge Guide: Check if an Element is Present in a Binary Search Tree

Check if an Element is Present in a Binary Search Tree


Solutions

Solution 1 (Click to Hide/Show)
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.isPresent = function(data, node = this.root) {
    if (node === null) {
      return false;
    } else if (data < node.value) {
      return this.isPresent(data, node.left);
    } else if (data > node.value) {
      return this.isPresent(data, node.right);
    } else {
      return true;
    }
  }
  // Only change code above this line
}
1 Like