-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy path0230-kth-smallest-element-in-a-bst.js
54 lines (43 loc) · 1.26 KB
/
0230-kth-smallest-element-in-a-bst.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// 230. Kth Smallest Element in a BST
// Medium 44%
// Given a binary search tree, write a function kthSmallest to find the kth
// smallest element in it.
// Note:
// You may assume k is always valid, 1 ≤ k ≤ BST's total elements.
// Follow up:
// What if the BST is modified (insert/delete operations) often and you need to
// find the kth smallest frequently? How would you optimize the kthSmallest
// routine?
// Credits:Special thanks to @ts for adding this problem and creating all test
// cases.
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @param {number} k
* @return {number}
*/
const kthSmallest = function(root, k) {
function inorder(root) {
if (!root) return null
const left = inorder(root.left)
if (left !== null) return left
if (--k <= 0) return root.val
return inorder(root.right)
}
return inorder(root)
}
const TreeNode = require('../structs/TreeNode')
;[
[[3,1,7,null,2,5,8,null,null,4,6], 4], // 4
].forEach(([array, k]) => {
console.log(kthSmallest(TreeNode.from(array), k))
})
// Solution:
// 中序遍历一棵二叉查找树就像顺序遍历一个排好序的数组。
// Submission Result: Accepted