Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5823b61

Browse files
committed
Create: 230-Kth-Smallest-Element-in-a-BST.ts
1 parent 00d015e commit 5823b61

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
let inDepth = (node, stack) => {
2+
if (node === null) return;
3+
4+
inDepth(node.left, stack);
5+
6+
stack.push(node.val);
7+
8+
inDepth(node.right, stack);
9+
};
10+
11+
function kthSmallest(root: TreeNode | null, k: number): number {
12+
let stack = [];
13+
14+
inDepth(root, stack);
15+
16+
return stack[k - 1];
17+
}

0 commit comments

Comments
 (0)