-
Notifications
You must be signed in to change notification settings - Fork 0
/
二叉搜索树的第k个节点.js
48 lines (45 loc) · 1.03 KB
/
二叉搜索树的第k个节点.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
/**
* 二叉搜索树的第 k 个节点
*
* 给定一棵二叉搜索树,请找出其中的第 k 小的节点
*
* 例如:(5,3,7,2,4,6,8) 中,按结点数值大小顺序第三小节点的值为 4
*
* 思路:二叉搜索树的中序遍历即排序后的节点,本题实际考察二叉树的遍历
*
*/
//递归实现
function KthNode(root, k) {
const arr = [];
loopThrough(root, arr);
if (k > 0 && k <= arr.length) {
return arr[k - 1];
}
return null;
}
function loopThrough(node, result) {
if (node) {
loopThrough(node.left, result);
result.push(node);
loopThrough(node.right, result);
}
}
//非递归实现
function KthNode(root, k) {
const result = [];
const stack = [];
let current = root;
while (stack.length || current) {
while (current) {
stack.push(current);
current = current.left;
}
current = stack.pop();
result.push(current.data);
current = current.right;
}
if (k > 0 && k <= result.length) {
return result[k - 1];
}
return null;
}