Skip to content

Commit 587c005

Browse files
committed
Added kthSamllestElement solution
1 parent 7aa73ea commit 587c005

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var kthSmallest = function (root, k) {
2+
let arr = [];
3+
inOrder(root, arr);
4+
5+
for (let i = 0; i < arr.length; i++) {
6+
if (i === k - 1) return arr[i];
7+
}
8+
};
9+
10+
const inOrder = (root, arr) => {
11+
if (!root) return;
12+
13+
inOrder(root.left, arr);
14+
arr.push(root.val);
15+
inOrder(root.right, arr);
16+
};
17+
18+
// TC: O(n)
19+
// SC: O(n)

0 commit comments

Comments
 (0)