-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKthSmallestElementInBST.java
More file actions
77 lines (62 loc) · 1.92 KB
/
KthSmallestElementInBST.java
File metadata and controls
77 lines (62 loc) · 1.92 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package binary_search_tree;
import java.util.*;
/**
* Description: https://leetcode.com/problems/kth-smallest-element-in-a-bst
* Difficulty: Medium
*/
public class KthSmallestElementInBST {
/**
* Time complexity: O(h + k)
* Space complexity: O(h)
*/
public int kthSmallestViaIterativeInorderTraversal(TreeNode root, int k) {
Deque<TreeNode> stack = new LinkedList<>();
while (root != null || !stack.isEmpty()) {
if (root != null) {
stack.push(root);
root = root.left;
} else {
root = stack.pop();
if (--k == 0) return root.val;
root = root.right;
}
}
return -1;
}
/**
* Time complexity: O(n)
* Space complexity: O(n)
*/
public int kthSmallestViaRecursiveInorderTraversal(TreeNode root, int k) {
List<Integer> inorder = new ArrayList<>();
traverseInorder(root, inorder);
return inorder.get(k - 1);
}
private void traverseInorder(TreeNode root, List<Integer> result) {
if (root == null) return;
traverseInorder(root.left, result);
result.add(root.val);
traverseInorder(root.right, result);
}
/**
* Time complexity: O(nlog k)
* Space complexity: O(h + k)
*/
public int kthSmallestViaMaxHeap(TreeNode root, int k) {
Queue<Integer> maxHeap = new PriorityQueue<>((a, b) -> Integer.compare(b, a));
backtrack(root, maxHeap, k);
return maxHeap.peek();
}
private void backtrack(TreeNode root, Queue<Integer> maxHeap, int k) {
if (root == null) return;
maxHeap.offer(root.val);
if (maxHeap.size() > k) maxHeap.poll();
backtrack(root.left, maxHeap, k);
backtrack(root.right, maxHeap, k);
}
private static class TreeNode {
int val;
TreeNode left;
TreeNode right;
}
}