Skip to content

Commit 62d1504

Browse files
refactor 235
1 parent 2913d8d commit 62d1504

File tree

2 files changed

+40
-59
lines changed

2 files changed

+40
-59
lines changed

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

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,4 @@ public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
3939
}
4040
}
4141

42-
public static class Solution2 {
43-
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
44-
if (p == root || q == root || p == q) {
45-
return root;
46-
}
47-
if (p.val < root.val && q.val > root.val) {
48-
return root;
49-
}
50-
if (p.val < root.val && q.val < root.val) {
51-
return lowestCommonAncestor(root.left, p, q);
52-
}
53-
if (p.val > root.val && q.val > root.val) {
54-
return lowestCommonAncestor(root.right, p, q);
55-
}
56-
return root;
57-
}
58-
}
59-
}
42+
}

src/test/java/com/fishercoder/_235Test.java

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,50 @@
33
import com.fishercoder.common.classes.TreeNode;
44
import com.fishercoder.common.utils.TreeUtils;
55
import com.fishercoder.solutions._235;
6+
67
import java.util.Arrays;
8+
79
import org.junit.BeforeClass;
810
import org.junit.Test;
911

1012
import static org.junit.Assert.assertEquals;
1113

1214
public class _235Test {
13-
private static _235.Solution1 solution1;
14-
private static _235.Solution2 solution2;
15-
private static TreeNode root;
16-
private static TreeNode p;
17-
private static TreeNode q;
18-
19-
@BeforeClass
20-
public static void setup() {
21-
solution1 = new _235.Solution1();
22-
solution2 = new _235.Solution2();
23-
}
24-
25-
@Test
26-
public void test1() {
27-
root = TreeUtils.constructBinaryTree(Arrays.asList(6, 2, 8, 0, 4, 7, 9, 3, 5));
28-
TreeUtils.printBinaryTree(root);
29-
30-
p = TreeUtils.constructBinaryTree(Arrays.asList(2, 0, 4, 3, 5));
31-
TreeUtils.printBinaryTree(p);
32-
33-
q = TreeUtils.constructBinaryTree(Arrays.asList(8, 7, 9));
34-
TreeUtils.printBinaryTree(q);
35-
36-
assertEquals(root, solution1.lowestCommonAncestor(root, p, q));
37-
assertEquals(root, solution2.lowestCommonAncestor(root, p, q));
38-
}
39-
40-
@Test
41-
public void test2() {
42-
root = TreeUtils.constructBinaryTree(Arrays.asList(6, 2, 8, 0, 4, 7, 9, 3, 5));
43-
TreeUtils.printBinaryTree(root);
44-
45-
p = TreeUtils.constructBinaryTree(Arrays.asList(2, 0, 4, 3, 5));
46-
TreeUtils.printBinaryTree(p);
47-
48-
q = TreeUtils.constructBinaryTree(Arrays.asList(4));
49-
TreeUtils.printBinaryTree(q);
50-
51-
assertEquals(p, solution1.lowestCommonAncestor(root, p, q));
52-
assertEquals(p, solution2.lowestCommonAncestor(root, p, q));
53-
}
15+
private static _235.Solution1 solution1;
16+
private static TreeNode root;
17+
private static TreeNode p;
18+
private static TreeNode q;
19+
20+
@BeforeClass
21+
public static void setup() {
22+
solution1 = new _235.Solution1();
23+
}
24+
25+
@Test
26+
public void test1() {
27+
root = TreeUtils.constructBinaryTree(Arrays.asList(6, 2, 8, 0, 4, 7, 9, 3, 5));
28+
TreeUtils.printBinaryTree(root);
29+
30+
p = TreeUtils.constructBinaryTree(Arrays.asList(2, 0, 4, 3, 5));
31+
TreeUtils.printBinaryTree(p);
32+
33+
q = TreeUtils.constructBinaryTree(Arrays.asList(8, 7, 9));
34+
TreeUtils.printBinaryTree(q);
35+
36+
assertEquals(root, solution1.lowestCommonAncestor(root, p, q));
37+
}
38+
39+
@Test
40+
public void test2() {
41+
root = TreeUtils.constructBinaryTree(Arrays.asList(6, 2, 8, 0, 4, 7, 9, 3, 5));
42+
TreeUtils.printBinaryTree(root);
43+
44+
p = TreeUtils.constructBinaryTree(Arrays.asList(2, 0, 4, 3, 5));
45+
TreeUtils.printBinaryTree(p);
46+
47+
q = TreeUtils.constructBinaryTree(Arrays.asList(4));
48+
TreeUtils.printBinaryTree(q);
49+
50+
assertEquals(p, solution1.lowestCommonAncestor(root, p, q));
51+
}
5452
}

0 commit comments

Comments
 (0)