diff --git a/Data Structures/bstMinMax.js b/Data Structures/bstMinMax.js new file mode 100644 index 0000000..4ba2eae --- /dev/null +++ b/Data Structures/bstMinMax.js @@ -0,0 +1,38 @@ +// https://www.freecodecamp.org/learn/coding-interview-prep/data-structures/find-the-minimum-and-maximum-value-in-a-binary-search-tree + +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.findMin = () => { + if (this.root === null) { + return null; + } + let node = this.root; + while (true) { + if (node.left === null) { + return node.value; + } + node = node.left; + } + }; + + this.findMax = () => { + if (this.root === null) { + return null; + } + let node = this.root; + while (true) { + if (node.right === null) { + return node.value; + } + node = node.right; + } + }; + // Only change code above this line +}