From 5da1322aaaacbf180d42bf5a38f5c949271c6686 Mon Sep 17 00:00:00 2001 From: Peter Chen Date: Sat, 8 Nov 2025 12:11:45 +0800 Subject: [PATCH] Add solution for "Path Sum" with unit tests. --- .../java/com/leetcode/easy/PathSum112.java | 26 ++++++++ .../com/leetcode/easy/PathSum112Test.java | 61 +++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 src/main/java/com/leetcode/easy/PathSum112.java create mode 100644 src/test/java/com/leetcode/easy/PathSum112Test.java diff --git a/src/main/java/com/leetcode/easy/PathSum112.java b/src/main/java/com/leetcode/easy/PathSum112.java new file mode 100644 index 0000000..c0be4d7 --- /dev/null +++ b/src/main/java/com/leetcode/easy/PathSum112.java @@ -0,0 +1,26 @@ +// Tags: Tree, DFS +package com.leetcode.easy; + +import com.leetcode.datastructure.TreeNode; + +public class PathSum112 { + /** + * Time complexity: we visit each node exactly once, + * thus the time complexity is O(N), where N is the number of nodes. + *

+ * Space complexity: in the worst case, + * the tree is completely unbalanced, + * e.g. each node has only one child node, + * the recursion call would occur N times (the height of the tree), + * therefore the storage to keep the call stack would be O(N). + */ + public boolean hasPathSum(TreeNode root, int targetSum) { + if (root == null) { + return false; + } + if (root.left == null && root.right == null && root.val == targetSum) { + return true; + } + return this.hasPathSum(root.left, targetSum - root.val) || this.hasPathSum(root.right, targetSum - root.val); + } +} diff --git a/src/test/java/com/leetcode/easy/PathSum112Test.java b/src/test/java/com/leetcode/easy/PathSum112Test.java new file mode 100644 index 0000000..353ba4b --- /dev/null +++ b/src/test/java/com/leetcode/easy/PathSum112Test.java @@ -0,0 +1,61 @@ +package com.leetcode.easy; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import com.leetcode.datastructure.TreeNode; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class PathSum112Test { + private PathSum112 solution; + + @BeforeEach + void setUp() { + solution = new PathSum112(); + } + + @Test + void testHasPathSum_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.right = new TreeNode(1); + + int targetSum = 22; + + boolean result = solution.hasPathSum(root, targetSum); + + assertTrue(result); + } + + @Test + void testHasPathSum_Example2() { + TreeNode root = new TreeNode(1); + root.left = new TreeNode(2); + root.right = new TreeNode(3); + + int targetSum = 5; + + boolean result = solution.hasPathSum(root, targetSum); + + assertFalse(result); + } + + @Test + void testHasPathSum_Example3() { + TreeNode root = null; + + int targetSum = 0; + + boolean result = solution.hasPathSum(root, targetSum); + + assertFalse(result); + } +}