freeCodeCamp/Data Structures/bstMinMax.js

39 lines
881 B
JavaScript
Raw Permalink Normal View History

// 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
}