Skip to content

Commit a8e6657

Browse files
refactor 98
1 parent 58b66c5 commit a8e6657

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

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

+22-19
Original file line numberDiff line numberDiff line change
@@ -26,36 +26,39 @@
2626
Binary tree [1,2,3], return false.
2727
*/
2828
public class _98 {
29-
class MoreConciseSolution {
29+
30+
public static class Solution1 {
3031

3132
public boolean isValidBST(TreeNode root) {
3233
return valid(root, null, null);
3334
}
3435

35-
private boolean valid(TreeNode root, Integer min, Integer max) {
36-
if (root == null) return true;
37-
if ((min != null && root.val <= min)
38-
|| (max != null && root.val >= max)) return false;
36+
boolean valid(TreeNode root, Integer min, Integer max) {
37+
if (root == null) {
38+
return true;
39+
}
40+
if ((min != null && root.val <= min) || (max != null && root.val >= max)) {
41+
return false;
42+
}
3943
return valid(root.left, min, root.val) && valid(root.right, root.val, max);
4044
}
4145
}
4246

4347

44-
public boolean isValidBST(TreeNode root) {
45-
if (root == null) return true;
46-
return dfs(root.left, Long.MIN_VALUE, root.val) && dfs(root.right, root.val, Long.MAX_VALUE);
47-
}
48+
public static class Solution2 {
49+
public boolean isValidBST(TreeNode root) {
50+
if (root == null) return true;
51+
return dfs(root.left, Long.MIN_VALUE, root.val) && dfs(root.right, root.val, Long.MAX_VALUE);
52+
}
4853

49-
private boolean dfs(TreeNode root, long minValue, long maxValue) {
50-
if (root == null) return true;
51-
if (root != null && (root.val <= minValue || root.val >= maxValue)) return false;
52-
boolean leftResult = true, rightResult = true;
53-
if (root.left != null) leftResult = dfs(root.left, minValue, root.val);
54-
if (root.right != null) rightResult = dfs(root.right, root.val, maxValue);
55-
return leftResult && rightResult;
54+
private boolean dfs(TreeNode root, long minValue, long maxValue) {
55+
if (root == null) return true;
56+
if (root != null && (root.val <= minValue || root.val >= maxValue)) return false;
57+
boolean leftResult = true, rightResult = true;
58+
if (root.left != null) leftResult = dfs(root.left, minValue, root.val);
59+
if (root.right != null) rightResult = dfs(root.right, root.val, maxValue);
60+
return leftResult && rightResult;
61+
}
5662
}
5763

58-
public static void main(String... args) {
59-
System.out.println(Integer.MAX_VALUE == 2147483647);
60-
}
6164
}

src/test/java/com/fishercoder/_98Test.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,32 @@
1111
* Created by fishercoder on 5/17/17.
1212
*/
1313
public class _98Test {
14-
private static _98 test;
14+
private static _98.Solution1 solution1;
1515
private static TreeNode root;
1616

1717
@BeforeClass
1818
public static void setup(){
19-
test = new _98();
19+
solution1 = new _98.Solution1();
2020
}
2121

2222
@Test
2323
public void test1(){
2424
root = new TreeNode(2);
2525
root.left = new TreeNode(1);
2626
root.right = new TreeNode(3);
27-
assertEquals(true, test.isValidBST(root));
27+
assertEquals(true, solution1.isValidBST(root));
2828
}
2929

3030
@Test
3131
public void test2(){
3232
root = new TreeNode(0);
33-
assertEquals(true, test.isValidBST(root));
33+
assertEquals(true, solution1.isValidBST(root));
3434
}
3535

3636
@Test
3737
public void test3(){
3838
root = new TreeNode(1);
3939
root.left = new TreeNode(1);
40-
assertEquals(false, test.isValidBST(root));
40+
assertEquals(false, solution1.isValidBST(root));
4141
}
4242
}

0 commit comments

Comments
 (0)