From 9bcc074f703794b7c8ce8f4d620878d56e9c5da3 Mon Sep 17 00:00:00 2001 From: Manish Date: Mon, 28 Aug 2023 10:42:48 +1000 Subject: [PATCH] Data Structures: Find the Minimum and Maximum Value in a Binary Search Tree --- Data Structures/bstMinMax.js | 38 ++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Data Structures/bstMinMax.js 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 +}