Recursive solution to binary search tree add

What is your hint or solution suggestion?
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 = (value) => {

const newNode = new Node(value);

if (this.root === null) this.root = newNode;

else {

  this.recursiveAdd(newNode, this.root);

}

}

this.recursiveAdd = (newNode, node) => {

if (newNode.value == node.value) return null;

//check each side, if we reached a leaf add node, else move on recursively

if (newNode.value < node.value) {

  node.left? this.recursiveAdd(newNode, node.left) : node.left = newNode;

}

else if (newNode.value > node.value) {

  node.right? this.recursiveAdd(newNode, node.right) : node.right = newNode;

}

}

// Only change code above this line

}

Challenge: Add a New Element to a Binary Search Tree

Link to the challenge:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.