10000 refactor 230 · codingwhite/Leetcode-4@369fe1f · GitHub
[go: up one dir, main page]

Skip to content

Commit 369fe1f

Browse files
refactor 230
1 parent aab634e commit 369fe1f

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ What if the BST is modified (insert/delete operations) often and you need to fin
1919
*/
2020
public class _230 {
2121

22-
public static class MostNaiveWay {
22+
public static class Solution1 {
2323
public int kthSmallest(TreeNode root, int k) {
2424
List<Integer> inorderList = new ArrayList<>();
2525
inorder(root, inorderList);
@@ -41,7 +41,10 @@ private void inorder(TreeNode root, List<Integer> inorderList) {
4141
}
4242
}
4343

44-
public static class BetterWay {
44+
public static class Solution2 {
45+
/**
46+
* Inorder traversal gives the natural ordering of a BST, no need to sort.
47+
*/
4548
int count = 0;
4649
int result = Integer.MIN_VALUE;
4750

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,31 @@
1111
* Created by fishercoder on 5/19/17.
1212
*/
1313
public class _230Test {
14-
private static _230.MostNaiveWay naiveWay;
15-
private static _230.BetterWay betterWay;
14+
private static _230.Solution1 solution1;
15+
private static _230.Solution2 solution2;
1616
private static TreeNode root;
1717
private static int k;
1818

1919
@BeforeClass
2020
public static void setup() {
21-
naiveWay = new _230.MostNaiveWay();
22-
betterWay = new _230.BetterWay();
21+
solution1 = new _230.Solution1();
22+
solution2 = new _230.Solution2();
2323
}
2424

2525
@Test
2626
public void test1() {
2727
root = new TreeNode(1);
2828
k = 1;
29-
assertEquals(1, naiveWay.kthSmallest(root, k));
30-
assertEquals(1, betterWay.kthSmallest(root, k));
29+
assertEquals(1, solution1.kthSmallest(root, k));
30+
assertEquals(1, solution2.kthSmallest(root, k));
3131
}
3232

3333
@Test
3434
public void test2() {
3535
root = new TreeNode(2);
3636
root.left = new TreeNode(1);
3737
k = 1;
38-
assertEquals(1, naiveWay.kthSmallest(root, k));
39-
assertEquals(1, betterWay.kthSmallest(root, k));
38+
assertEquals(1, solution1.kthSmallest(root, k));
39+
assertEquals(1, solution2.kthSmallest(root, k));
4040
}
4141
}

0 commit comments

Comments
 (0)
0