From a502346776409995821bfd26e59dca7e3ddcebc7 Mon Sep 17 00:00:00 2001 From: Felix Kwan Date: Thu, 12 Oct 2023 17:59:24 +0800 Subject: [PATCH] New Problem Solution -"Kth Smallest Element in a BST" --- README.md | 2 +- .../KthSmallestElementInABST/kthSmallest.js | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 algorithms/javascript/KthSmallestElementInABST/kthSmallest.js diff --git a/README.md b/README.md index 2bf93af3..86662340 100644 --- a/README.md +++ b/README.md @@ -348,7 +348,7 @@ LeetCode |233|[Number of Digit One](https://leetcode.com/problems/number-of-digit-one/)| [C++](./algorithms/cpp/numberOfDigitOne/NumberOfDigitOne.cpp)|Medium| |232|[Implement Queue using Stacks](https://leetcode.com/problems/implement-queue-using-stacks/)| [C++](./algorithms/cpp/implementQueueUsingStacks/ImplementQueueUsingStacks.cpp), [Java](./algorithms/java/src/myQueue/MyQueue.java)|Easy| |231|[Power of Two](https://leetcode.com/problems/power-of-two/)| [C++](./algorithms/cpp/powerOfTwo/PowerOfTwo.cpp)|Easy| -|230|[Kth Smallest Element in a BST](https://leetcode.com/problems/kth-smallest-element-in-a-bst/)| [C++](./algorithms/cpp/kthSmallestElementInaBST/KthSmallestElementInABst.cpp), [Python](./algorithms/python/KthSmallestElementInABST/kthSmallest.py)|Medium| +|230|[Kth Smallest Element in a BST](https://leetcode.com/problems/kth-smallest-element-in-a-bst/)| [C++](./algorithms/cpp/kthSmallestElementInaBST/KthSmallestElementInABst.cpp), [Python](./algorithms/python/KthSmallestElementInABST/kthSmallest.py), [Javascript](./algorithms/javascript/KthSmallestElementInABST/kthSmallest.js)|Medium| |229|[Majority Element II](https://leetcode.com/problems/majority-element-ii/) | [C++](./algorithms/cpp/majorityElement/majorityElement.II.cpp)|Medium| |228|[Summary Ranges](https://leetcode.com/problems/summary-ranges/)| [C++](./algorithms/cpp/summaryRanges/SummaryRanges.cpp)|Easy| |227|[Basic Calculator II](https://leetcode.com/problems/basic-calculator-ii/)| [C++](./algorithms/cpp/basicCalculator/BasicCalculator.II.cpp)|Medium| diff --git a/algorithms/javascript/KthSmallestElementInABST/kthSmallest.js b/algorithms/javascript/KthSmallestElementInABST/kthSmallest.js new file mode 100644 index 00000000..d243b7fe --- /dev/null +++ b/algorithms/javascript/KthSmallestElementInABST/kthSmallest.js @@ -0,0 +1,16 @@ +/** + * @param {TreeNode} root + * @param {number} k + * @return {number} + */ +var kthSmallest = function(root, k) { + let vals = []; + (function dfs(node) { + if (vals.length != k) { //no need to keep going after reach k-th number + if (node.left) dfs(node.left); //go left first + vals.push(node.val); //finished going left, now start adding values + if (node.right) dfs(node.right); //if have right, go there and repeat process + } + })(root) // IFFE Immediately Invoking Function Expression, starting from root. + return vals[k-1]; //return element, but as i mentioned in the descript, don't need this full array, just the k-th elm + }; \ No newline at end of file