From f79a6cda6d29aa4b132abba790218d93e397d9cd Mon Sep 17 00:00:00 2001 From: Peter Chen Date: Sat, 6 Sep 2025 15:20:42 +0800 Subject: [PATCH] Add unit tests for the "Path Sum II" problem. --- .../com/leetcode/medium}/PathSumII113.java | 19 ++-- .../com/leetcode/medium/PathSumII113Test.java | 86 +++++++++++++++++++ 2 files changed, 94 insertions(+), 11 deletions(-) rename src/{ => main/java/com/leetcode/medium}/PathSumII113.java (68%) create mode 100644 src/test/java/com/leetcode/medium/PathSumII113Test.java diff --git a/src/PathSumII113.java b/src/main/java/com/leetcode/medium/PathSumII113.java similarity index 68% rename from src/PathSumII113.java rename to src/main/java/com/leetcode/medium/PathSumII113.java index 89a60fc..926efa6 100644 --- a/src/PathSumII113.java +++ b/src/main/java/com/leetcode/medium/PathSumII113.java @@ -1,7 +1,10 @@ -// Tree, Backtracking, Depth-First Search, Binary Tree +// Tags: Tree, DFS, Backtracking +package com.leetcode.medium; + +import com.leetcode.datastructure.TreeNode; -import java.util.List; import java.util.ArrayList; +import java.util.List; public class PathSumII113 { private void dfs(TreeNode node, List> res, List path, int remainingSum) { @@ -14,8 +17,9 @@ private void dfs(TreeNode node, List> res, List path, int } this.dfs(node.left, res, path, remainingSum - node.val); this.dfs(node.right, res, path, remainingSum - node.val); - path.remove(path.size() - 1); + path.removeLast(); } + /* Time Complexity: O(N^2) where N are the number of nodes in a tree. @@ -29,14 +33,7 @@ Hence, the complexity in the worst case could be O(N^2). */ public List> pathSum(TreeNode root, int targetSum) { List> res = new ArrayList<>(); - List path = new ArrayList<>(); - this.dfs(root, res, path, targetSum); + this.dfs(root, res, new ArrayList<>(), targetSum); return res; } - - public static void main(String[] args) { - TreeNode root = new TreeNode(5, new TreeNode(4, new TreeNode(11, new TreeNode(7), new TreeNode(2)), null), new TreeNode(8, new TreeNode(13), new TreeNode(4, new TreeNode(5), new TreeNode(1)))); - int targetSum = 22; - System.out.println(new PathSumII113().pathSum(root, targetSum)); // [[5, 4, 11, 2], [5, 8, 4, 5]] - } } diff --git a/src/test/java/com/leetcode/medium/PathSumII113Test.java b/src/test/java/com/leetcode/medium/PathSumII113Test.java new file mode 100644 index 0000000..4899ba4 --- /dev/null +++ b/src/test/java/com/leetcode/medium/PathSumII113Test.java @@ -0,0 +1,86 @@ +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> expected = List.of( + List.of(5, 4, 11, 2), + List.of(5, 8, 4, 5) + ); + + List> result = solution.pathSum(root, targetSum); + + 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> expected = List.of(); + + List> 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> expected = List.of(); + + List> 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> expected = List.of(); + + List> result = solution.pathSum(root, targetSum); + + assertEquals(expected, result); + } +}