Repository navigation
Expand file tree
/
Copy pathPathSumII113Test.java
More file actions
94 lines (68 loc) · 2.4 KB
/
Copy pathPathSumII113Test.java
File metadata and controls
94 lines (68 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.leetcode.medium;
import com.leetcode.datastructure.TreeNode;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class PathSumII113Test {
private PathSumII113 solution;
@BeforeEach
public void setUp() {
solution = new PathSumII113();
}
@Test
void testPathSum_Example1() {
TreeNode root = new TreeNode(5);
root.left = new TreeNode(4);
root.right = new TreeNode(8);
root.left.left = new TreeNode(11);
root.left.left.left = new TreeNode(7);
root.left.left.right = new TreeNode(2);
root.right.left = new TreeNode(13);
root.right.right = new TreeNode(4);
root.right.right.left = new TreeNode(5);
root.right.right.right = new TreeNode(1);
int targetSum = 22;
List<List<Integer>> expected = List.of(
List.of(5, 4, 11, 2),
List.of(5, 8, 4, 5)
);
List<List<Integer>> result = solution.pathSum(root, targetSum);
result.sort((a, b) -> {
for (int i = 0; i < Math.min(a.size(), b.size()); i++) {
int cmp = Integer.compare(a.get(i), b.get(i));
if (cmp != 0) return cmp;
}
return Integer.compare(a.size(), b.size());
});
assertEquals(expected, result);
}
@Test
void testPathSum_Example2() {
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
int targetSum = 5;
List<List<Integer>> expected = List.of();
List<List<Integer>> result = solution.pathSum(root, targetSum);
assertEquals(expected, result);
}
@Test
void testPathSum_Example3() {
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
int targetSum = 0;
List<List<Integer>> expected = List.of();
List<List<Integer>> result = solution.pathSum(root, targetSum);
assertEquals(expected, result);
}
@Test
void testPathSum_Example4() {
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
int targetSum = 1;
List<List<Integer>> expected = List.of();
List<List<Integer>> result = solution.pathSum(root, targetSum);
assertEquals(expected, result);
}
}