Skip to content

Commit 3493dae

Browse files
add tests for 98
1 parent 017d79f commit 3493dae

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@
2424
Binary tree [1,2,3], return false.
2525
*/
2626
public class _98 {
27+
class MoreConciseSolution {
28+
29+
public boolean isValidBST(TreeNode root) {
30+
return valid(root, null, null);
31+
}
32+
33+
private boolean valid(TreeNode root, Integer min, Integer max) {
34+
if (root == null) return true;
35+
if ((min != null && root.val <= min)
36+
|| (max != null && root.val >= max)) return false;
37+
return valid(root.left, min, root.val) && valid(root.right, root.val, max);
38+
}
39+
}
40+
41+
2742
public boolean isValidBST(TreeNode root) {
2843
if (root == null) return true;
2944
return dfs(root.left, Long.MIN_VALUE, root.val) && dfs(root.right, root.val, Long.MAX_VALUE);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.stevesun;
2+
3+
import com.stevesun.common.classes.TreeNode;
4+
import com.stevesun.solutions._48;
5+
import com.stevesun.solutions._98;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import static org.junit.Assert.assertEquals;
10+
11+
/**
12+
* Created by stevesun on 5/17/17.
13+
*/
14+
public class _98Test {
15+
private static _98 test;
16+
private static TreeNode root;
17+
18+
@BeforeClass
19+
public static void setup(){
20+
test = new _98();
21+
}
22+
23+
@Test
24+
public void test1(){
25+
root = new TreeNode(2);
26+
root.left = new TreeNode(1);
27+
root.right = new TreeNode(3);
28+
assertEquals(true, test.isValidBST(root));
29+
}
30+
31+
@Test
32+
public void test2(){
33+
root = new TreeNode(0);
34+
assertEquals(true, test.isValidBST(root));
35+
}
36+
37+
@Test
38+
public void test3(){
39+
root = new TreeNode(1);
40+
root.left = new TreeNode(1);
41+
assertEquals(false, test.isValidBST(root));
42+
}
43+
}

0 commit comments

Comments
 (0)