Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 703cd8a

Browse files
refactor 230
1 parent 64d96fe commit 703cd8a

File tree

2 files changed

+8
-49
lines changed

2 files changed

+8
-49
lines changed

src/main/java/com/fishercoder/solutions/_230.java

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,65 +5,28 @@
55
import java.util.ArrayList;
66
import java.util.List;
77

8-
/**
9-
* 230. Kth Smallest Element in a BST
10-
*
11-
* Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.
12-
13-
Note:
14-
You may assume k is always valid, 1 ? k ? BST's total elements.
15-
16-
Follow up:
17-
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?
18-
19-
*/
208
public class _230 {
219

2210
public static class Solution1 {
23-
public int kthSmallest(TreeNode root, int k) {
24-
List<Integer> inorderList = new ArrayList<>();
25-
inorder(root, inorderList);
26-
return inorderList.get(k - 1);
27-
}
28-
29-
private void inorder(TreeNode root, List<Integer> inorderList) {
30-
if (root == null) {
31-
return;
32-
}
33-
if (root.left != null) {
34-
inorder(root.left, inorderList);
35-
}
36-
inorderList.add(root.val);
37-
if (root.right != null) {
38-
inorder(root.right, inorderList);
39-
}
40-
return;
41-
}
42-
}
43-
44-
public static class Solution2 {
4511
/**
4612
* Inorder traversal gives the natural ordering of a BST, no need to sort.
4713
*/
48-
int count = 0;
49-
int result = Integer.MIN_VALUE;
50-
5114
public int kthSmallest(TreeNode root, int k) {
52-
inorder(root, k);
53-
return result;
15+
List<Integer> inorder = new ArrayList();
16+
dfs(root, inorder, k);
17+
return inorder.get(k - 1);
5418
}
5519

56-
private void inorder(TreeNode root, int k) {
20+
private void dfs(TreeNode root, List<Integer> list, int k) {
5721
if (root == null) {
5822
return;
5923
}
60-
inorder(root.left, k);
61-
count++;
62-
if (count == k) {
63-
result = root.val;
24+
dfs(root.left, list, k);
25+
list.add(root.val);
26+
dfs(root.right, list, k);
27+
if (list.size() >= (k - 1)) {
6428
return;
6529
}
66-
inorder(root.right, k);
6730
}
6831
}
6932

src/test/java/com/fishercoder/_230Test.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,19 @@
1212
*/
1313
public class _230Test {
1414
private static _230.Solution1 solution1;
15-
private static _230.Solution2 solution2;
1615
private static TreeNode root;
1716
private static int k;
1817

1918
@BeforeClass
2019
public static void setup() {
2120
solution1 = new _230.Solution1();
22-
solution2 = new _230.Solution2();
2321
}
2422

2523
@Test
2624
public void test1() {
2725
root = new TreeNode(1);
2826
k = 1;
2927
assertEquals(1, solution1.kthSmallest(root, k));
30-
assertEquals(1, solution2.kthSmallest(root, k));
3128
}
3229

3330
@Test
@@ -36,6 +33,5 @@ public void test2() {
3633
root.left = new TreeNode(1);
3734
k = 1;
3835
assertEquals(1, solution1.kthSmallest(root, k));
39-
assertEquals(1, solution2.kthSmallest(root, k));
4036
}
4137
}

0 commit comments

Comments
 (0)