We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f8729d1 commit 1bd2030Copy full SHA for 1bd2030
kth-smallest-element-in-a-bst/wooseok123.js
@@ -0,0 +1,19 @@
1
+// TC : O(n log n) | SC : O(n)
2
+
3
+let findAllValuesInTree = (root, obj) => {
4
+ obj[root.val] = true;
5
+ if (!root.left && !root.right) return obj;
6
+ if (root.left) findAllValuesInTree(root.left, obj);
7
+ if (root.right) findAllValuesInTree(root.right, obj);
8
9
+ return obj;
10
+};
11
12
+var kthSmallest = function (root, k) {
13
+ const obj = findAllValuesInTree(root, {});
14
+ const sortedList = Object.keys(obj)
15
+ .map(Number)
16
+ .sort((a, b) => a - b);
17
18
+ return sortedList[k - 1];
19
0 commit comments