Skip to content

Commit b06d8c3

Browse files
authored
Added unit tests 111-115.
1 parent eef1ccc commit b06d8c3

File tree

5 files changed

+61
-53
lines changed

5 files changed

+61
-53
lines changed

src.save/test/java/g0101_0200/s0111_minimum_depth_of_binary_tree/SolutionTest.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,21 @@
44
import static org.hamcrest.MatcherAssert.assertThat;
55

66
import com_github_leetcode.TreeNode;
7+
import com_github_leetcode.TreeUtils;
8+
import java.util.Arrays;
79
import org.junit.jupiter.api.Test;
810

911
class SolutionTest {
1012
@Test
1113
void minDepth() {
12-
TreeNode bottomLeft = new TreeNode(15);
13-
TreeNode bottomRight = new TreeNode(7);
14-
15-
TreeNode upRight = new TreeNode(20, bottomLeft, bottomRight);
16-
TreeNode upLeft = new TreeNode(9);
17-
TreeNode root = new TreeNode(3, upLeft, upRight);
18-
14+
TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(3, 9, 20, null, null, 15, 7));
1915
assertThat(new Solution().minDepth(root), equalTo(2));
2016
}
17+
18+
@Test
19+
void minDepth2() {
20+
TreeNode root =
21+
TreeUtils.constructBinaryTree(Arrays.asList(2, null, 3, null, 4, null, 5, null, 6));
22+
assertThat(new Solution().minDepth(root), equalTo(5));
23+
}
2124
}

src.save/test/java/g0101_0200/s0112_path_sum/SolutionTest.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,27 @@
44
import static org.hamcrest.MatcherAssert.assertThat;
55

66
import com_github_leetcode.TreeNode;
7+
import com_github_leetcode.TreeUtils;
8+
import java.util.Arrays;
79
import org.junit.jupiter.api.Test;
810

911
class SolutionTest {
1012
@Test
11-
void pathSum() {
12-
TreeNode fourthLevelLeftMostLeft = new TreeNode(7);
13-
TreeNode fourthLevelLeftMostRight = new TreeNode(2);
14-
15-
TreeNode thirtLevelLeftMostRoot =
16-
new TreeNode(11, fourthLevelLeftMostLeft, fourthLevelLeftMostRight);
17-
18-
TreeNode secondLevelLeftMostRoot = new TreeNode(4, thirtLevelLeftMostRoot, null);
19-
20-
TreeNode fourthLevelRightMostRight = new TreeNode(1);
21-
TreeNode thirdLevelRightRoot = new TreeNode(4, fourthLevelRightMostRight, null);
22-
TreeNode thirdLevelLeftRoot = new TreeNode(13);
23-
24-
TreeNode secondLevelRightRoot = new TreeNode(8, thirdLevelLeftRoot, thirdLevelRightRoot);
13+
void hasPathSum() {
14+
TreeNode root =
15+
TreeUtils.constructBinaryTree(
16+
Arrays.asList(5, 4, 8, 11, null, 13, 4, 7, 2, null, null, null, 1));
17+
assertThat(new Solution().hasPathSum(root, 22), equalTo(true));
18+
}
2519

26-
TreeNode root = new TreeNode(5, secondLevelLeftMostRoot, secondLevelRightRoot);
20+
@Test
21+
void hasPathSum2() {
22+
TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3));
23+
assertThat(new Solution().hasPathSum(root, 22), equalTo(false));
24+
}
2725

28-
assertThat(new Solution().hasPathSum(root, 22), equalTo(true));
26+
@Test
27+
void hasPathSum3() {
28+
assertThat(new Solution().hasPathSum(null, 0), equalTo(false));
2929
}
3030
}

src.save/test/java/g0101_0200/s0113_path_sum_ii/SolutionTest.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,30 @@
55

66
import com_github_leetcode.ArrayUtils;
77
import com_github_leetcode.TreeNode;
8+
import com_github_leetcode.TreeUtils;
9+
import java.util.Arrays;
810
import org.junit.jupiter.api.Test;
911

1012
class SolutionTest {
1113
@Test
1214
void pathSum() {
13-
TreeNode fourthLevelLeftMostLeft = new TreeNode(7);
14-
TreeNode fourthLevelLeftMostRight = new TreeNode(2);
15-
16-
TreeNode thirtLevelLeftMostRoot =
17-
new TreeNode(11, fourthLevelLeftMostLeft, fourthLevelLeftMostRight);
18-
19-
TreeNode secondLevelLeftMostRoot = new TreeNode(4, thirtLevelLeftMostRoot, null);
20-
21-
TreeNode fourthLevelRightMostRight = new TreeNode(1);
22-
TreeNode fourthLevelRightMostLeft = new TreeNode(5);
23-
24-
TreeNode thirdLevelRightRoot =
25-
new TreeNode(4, fourthLevelRightMostRight, fourthLevelRightMostLeft);
26-
TreeNode thirdLevelLeftRoot = new TreeNode(13);
27-
28-
TreeNode secondLevelRightRoot = new TreeNode(8, thirdLevelLeftRoot, thirdLevelRightRoot);
29-
30-
TreeNode root = new TreeNode(5, secondLevelLeftMostRoot, secondLevelRightRoot);
15+
TreeNode root =
16+
TreeUtils.constructBinaryTree(
17+
Arrays.asList(5, 4, 8, 11, null, 13, 4, 7, 2, null, null, 5, 1));
3118
assertThat(
3219
new Solution().pathSum(root, 22),
3320
equalTo(ArrayUtils.getLists(new int[][] {{5, 4, 11, 2}, {5, 8, 4, 5}})));
3421
}
22+
23+
@Test
24+
void pathSum2() {
25+
TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3));
26+
assertThat(new Solution().pathSum(root, 5), equalTo(ArrayUtils.getLists(new int[][] {})));
27+
}
28+
29+
@Test
30+
void pathSum3() {
31+
TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2));
32+
assertThat(new Solution().pathSum(root, 0), equalTo(ArrayUtils.getLists(new int[][] {})));
33+
}
3534
}

src.save/test/java/g0101_0200/s0114_flatten_binary_tree_to_linked_list/SolutionTest.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,23 @@
44
import static org.hamcrest.MatcherAssert.assertThat;
55

66
import com_github_leetcode.TreeNode;
7+
import com_github_leetcode.TreeUtils;
8+
import java.util.Arrays;
9+
import java.util.Collections;
710
import org.junit.jupiter.api.Test;
811

912
class SolutionTest {
1013
@Test
11-
void flattenBinaryTreetoLinkedList() {
12-
TreeNode leftMostLeft = new TreeNode(3);
13-
TreeNode leftMostRight = new TreeNode(4);
14-
TreeNode leftRoot = new TreeNode(2, leftMostLeft, leftMostRight);
15-
16-
TreeNode rightMostRight = new TreeNode(6);
17-
TreeNode rightRoot = new TreeNode(5, null, rightMostRight);
18-
19-
TreeNode root = new TreeNode(1, leftRoot, rightRoot);
20-
14+
void flatten() {
15+
TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 5, 3, 4, null, 6));
2116
new Solution().flatten(root);
22-
2317
assertThat(root.toString(), equalTo("1,null,2,null,3,null,4,null,5,null,6"));
2418
}
19+
20+
@Test
21+
void flatten2() {
22+
TreeNode root = TreeUtils.constructBinaryTree(Collections.singletonList(0));
23+
new Solution().flatten(root);
24+
assertThat(root.toString(), equalTo("0"));
25+
}
2526
}

src.save/test/java/g0101_0200/s0115_distinct_subsequences/SolutionTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77

88
class SolutionTest {
99
@Test
10-
void distinctSubsequences() {
10+
void numDistinct() {
1111
assertThat(new Solution().numDistinct("rabbbit", "rabbit"), equalTo(3));
1212
}
13+
14+
@Test
15+
void numDistinct2() {
16+
assertThat(new Solution().numDistinct("babgbag", "bag"), equalTo(5));
17+
}
1318
}

0 commit comments

Comments
 (0)