Data Structures: Find the Minimum and Maximum Value in a Binary Search Tree

This commit is contained in:
Manish 2023-08-28 10:42:48 +10:00
parent f74eb15489
commit 9bcc074f70

View File

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