Data Structures: Find the Minimum and Maximum Value in a Binary Search Tree
This commit is contained in:
parent
f74eb15489
commit
9bcc074f70
38
Data Structures/bstMinMax.js
Normal file
38
Data Structures/bstMinMax.js
Normal 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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user