0%

Leetcode 230 Kth Smallest Element In A Bst

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.

Example 1:

1
2
3
4
5
6
7
Input: root = [3,1,4,null,2], k = 1
3
/ \
1 4
\
2
Output: 1

Example 2:

1
2
3
4
5
6
7
8
9
Input: root = [5,3,6,2,4,null,null,1], k = 3
5
/ \
3 6
/ \
2 4
/
1
Output: 3

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?

Constraints:

  • The number of elements of the BST is between 1 to 10^4.
  • You may assume k is always valid, 1 ≤ k ≤ BST's total elements.

非递归中序遍历

简单来说, 这题就是找到BST中序遍历的第k个值.

由于递归方法不太方便传入一个全局计数器, 所以采用非递归中序遍历.

维持一个变量currRank, 每遍历一个元素就++currRank.

当currRank = k的时候就返回

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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int kthSmallest(TreeNode root, int k) {
int currRank = 0;
Deque<TreeNode> stk = new ArrayDeque<>();
TreeNode tmp = root;
while(tmp != null)
{
stk.offerFirst(tmp);
tmp = tmp.left;
}
while(!stk.isEmpty())
{
TreeNode curr = stk.pollFirst();
++currRank;
if(currRank == k)
return curr.val;
TreeNode tmp1 = curr.right;
while(tmp1 != null)
{
stk.offerFirst(tmp1);
tmp1 = tmp1.left;
}
}
return -1; // if k > tree.size, return -1
}
}