-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKthSmallestElementInBST.java
More file actions
283 lines (201 loc) · 7.59 KB
/
KthSmallestElementInBST.java
File metadata and controls
283 lines (201 loc) · 7.59 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package Algorithms.BinaryTrees;
import java.util.*;
/**
* THOUGHTS
* --------
* 1) APPROACH 1: add all vals in PQ and for(int i=1; i<=k; i++) pq.pop()
* 2) APPROACH 2:
* In BST = inOrderTrav is ascending order?? NO
* In BST traverse through small path and maintain a queue with k size by inserting the trav values and once you reach the end then .poll() the last ele
*
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 01 Feb 2025
* @link 230. Kth Smallest Element in a BST <a href="https://leetcode.com/problems/kth-smallest-element-in-a-bst/">LeetCode Link</a>
* @topics Tree, BinaryTree, BinarySearchTree, DFS, InOrderTraversal
* @companies Microsoft(2), Oracle(2), Uber(2), Google(8), Amazon(5), Meta(2), Bloomberg(2), LinkedIn(2), TikTok(2), Cisco(2)
*/
public class KthSmallestElementInBST {
public static class TreeNode {int val;TreeNode left, right;TreeNode() {}TreeNode(int val) { this.val = val; }TreeNode(int val, TreeNode left, TreeNode right) {this.val = val;this.left = left;this.right = right;}}
public static void main(String[] args) {
TreeNode root = new TreeNode(3);
TreeNode one = new TreeNode(1);
TreeNode four = new TreeNode(4);
TreeNode two = new TreeNode(2);
/*
* 3
* / \
* 1 4
* \
* 2
*/
root.left = one;
root.right = four;
one.right = two;
System.out.println("kthSmallest Using In-Order Dfs Recursion 1: " + kthSmallestUsingInOrderDfsRecursion1(root, 1));
}
private static int i;
private static Integer res;
public static int kthSmallestUsingInOrderDfsRecursion1(TreeNode root, int k) {
i=0;
dfs(root, k);
return res;
}
private static void dfs(TreeNode node, int k) {
if (node == null || res != null) return;
dfs(node.left, k);
if (++i == k) res = node.val;
// System.out.printf("node: %s, i: %s, k : %s\n", node.val, i, k);
dfs(node.right, k);
}
public static int kthSmallestUsingInOrderDfsRecursion2(TreeNode root, int k) {
Stack<TreeNode> stack = new Stack<>(); // parentStack
while (!stack.isEmpty() || root != null) {
while (root != null) {
// System.out.println(root.val); // pre-order
stack.push(root);
root = root.left;
}
root = stack.pop();
// System.out.println(root.val); // in-order
if (--k == 0) break; // or return root.val;
root = root.right;
}
return root.val;
}
public static int kthSmallestUsingInOrderDfsAndStack(TreeNode root, int k) {
Stack<Integer> stack = new Stack<>();
dfs(root, k, stack);
return stack.pop();
}
private static void dfs(TreeNode node, int k, Stack<Integer> stack) {
// if (node == null) return; // O(n) time
if (node == null || stack.size() == k) return; // -> O(h) time
dfs(node.left, k, stack);
stack.push(node.val);
if (stack.size() > k) stack.pop();
dfs(node.right, k, stack);
}
public static int kthSmallestUsingMorrisTraversal(TreeNode root, int k) {
TreeNode curr = root;
while (curr != null) {
if (curr.left == null) {
if (--k == 0) break; // BREAK 1 ---
curr = curr.right;
} else {
TreeNode prev = curr.left; // predecessor
while (prev.right != null && prev.right != curr) {
prev = prev.right;
}
if (prev.right == null) { // create thread
prev.right = curr;
curr = curr.left;
} else { // remove thread
prev.right = null;
if (--k == 0) break; // BREAK 2 ---
curr = curr.right;
}
}
}
return curr.val;
}
public static int kthSmallestUsingInOrderDfsAndPq(TreeNode root, int k) {
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
dfs(root, k, pq);
return pq.poll();
}
private static void dfs(TreeNode node, int k, PriorityQueue<Integer> pq) {
if (node == null || pq.size() == k) return;
dfs(node.left, k, pq);
pq.add(node.val);
if (pq.size() > k) pq.poll();
dfs(node.right, k, pq);
}
public static int kthSmallestUsingInOrderList(TreeNode root, int k) {
ArrayList<Integer> list=new ArrayList<>();
inOrderList(root,list);
return list.get(k-1);
}
public static void inOrderList(TreeNode root,ArrayList<Integer> list){
if(root==null) return;
inOrderList(root.left, list);
list.add(root.val);
inOrderList(root.right, list);
}
public static int kthSmallestUsingInOrderQueue(TreeNode root, int k) {
Queue<Integer> q = new LinkedList<>();
inOrderQueue(root, q);
for (int i = 1; i<k; i++) q.poll();
return q.poll();
}
private static void inOrderQueue(TreeNode node, Queue<Integer> q) {
if (node == null) return;
inOrderQueue(node.left, q);
q.add(node.val);
inOrderQueue(node.right, q);
}
public static int kthSmallestUsingInOrderDfsRecursion3(TreeNode root, int k) {
Stack<TreeNode> stack = new Stack<>();
while (!stack.isEmpty() || root != null) {
if (root != null) {
stack.push(root);
root = root.left;
} else {
root = stack.pop();
// System.out.println(root.val);
if (--k == 0) break;
root = root.right;
}
}
return root.val;
}
public static int kthSmallestUsingInOrderDfsRecursion4(TreeNode root, int k) {
Stack<TreeNode> stack = new Stack<>(); // parentStack
mainLoop:
while (root != null) {
while (root != null) {
// System.out.println(root.val); // pre-order
stack.push(root);
root = root.left;
}
while (!stack.isEmpty() && root == null) {
root = stack.pop();
// System.out.println(root.val); // in-order
if (--k == 0) break mainLoop; // or return root.val;
root = root.right;
}
}
return root.val;
}
public static int kthSmallestUsingInOrderDfsRecursion5(TreeNode root, int k) {
LinkedList<TreeNode> stack = new LinkedList<>(); // parentStack
while (true) { // or while (!stack.isEmpty() || root != null) {
while (root != null) {
// System.out.println(root.val);
stack.push(root);
root = root.left;
}
root = stack.pop();
// System.out.println(root.val);
if (--k == 0) return root.val; // or break;
root = root.right;
}
}
/**
* WON'T WORK
*
* 1
* \
* 2
*
* here when k=2 then kth smallest is 2 which is on right side not left child tree
*/
public int kthSmallest2(TreeNode root, int k) {
Queue<Integer> q = new LinkedList<>();
TreeNode trav = root;
for (trav = root; trav!=null; trav = trav.left) {
q.add(trav.val);
if (q.size() > k) q.poll();
}
return q.poll();
}
}