leetcode解题: Kth Smallest Element in a BST (230)

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?

Hint:

Try to utilize the property of a BST.
What if you could modify the BST node’s structure?
The optimal runtime complexity is O(height of BST).

解法1: In-order traversal O(N), N is number of elements

这题可以看成是一个in-order traversal的直接应用,因为BST的in-order traversal是一个有序数组,那么我们就可以根据这个性质,对数进行遍历直到找到第k个node。
C++

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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int kthSmallest(TreeNode* root, int k) {
// implement a in-order-traversal
int count = 0;
int res;
stack<TreeNode*> s;
TreeNode* cur = root;
while (cur || !s.empty()) {
if (cur) {
s.push(cur);
cur = cur->left;
} else {
++count;
TreeNode* temp = s.top();
s.pop();
if (count == k) {
res = temp->val;
break;
}
cur = temp->right;
}
}
return res;
}
};

Java

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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int kthSmallest(TreeNode root, int k) {
// in-order traversal iterative solution
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode current = root;
int count = 0;
int val = 0;
while ( current != null || !stack.isEmpty()) {
if (current != null) {
stack.push(current);
current = current.left;
} else {
TreeNode temp = stack.pop();
val = temp.val;
count++;
current = temp.right;
if (count == k) {
return val;
}
}
}
return val;
}
}

Follow up : O(logN)

参考这篇文章的思路
如果可以修改每一个tree的node的结构,而使其保存leftcount和totalcount。那么一开始建立树需要O(N)的时间,而之后每一次insert和查找则只需要O(logN)的时间。

lang: java
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
class Solution {
class SuperNode {
int val;
int count;
int total;
SuperNode left;
SuperNode right;
public SuperNode(int val) {
this.val = val;
count = 0;
total = 1;
left = null;
right = null;
}
};
public SuperNode build(TreeNode root) {
if (root == null) return null;
SuperNode superRoot = new SuperNode(root.val);
SuperNode left = build(root.left);
SuperNode right = build(root.right);
superRoot.left = left;
superRoot.right = right;
if (left != null) {
superRoot.count = left.total;
} else {
superRoot.count = 0;
}
superRoot.total += ((left == null ? 0 : left.total) + (right == null ? 0 : right.total));
return superRoot;
}
public int kthSmallest(TreeNode root, int k) {
SuperNode superRoot = build(root);
return kth(superRoot, k);
}
private int kth(SuperNode root, int k) {
if (root == null || k <= 0) {
return 0;
}
if (k == root.count + 1) {
return root.val;
} else if (k < root.count + 1) {
return kth(root.left, k);
} else {
return kth(root.right, k - root.count - 1);
}
}
}