|
| 1 | +Sure! Here's the complete LeetCode-style `README.md` file for **437. Path Sum III**, in the format consistent with the ones from the `doocs/leetcode` GitHub repository. |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +```markdown |
| 6 | +# 437. Path Sum III |
| 7 | + |
| 8 | +## Description |
| 9 | + |
| 10 | +Given the `root` of a binary tree and an integer `targetSum`, return *the number of paths where the sum of the values along the path equals* `targetSum`. |
| 11 | + |
| 12 | +The path does not need to start or end at the root or a leaf, but it must go downwards (i.e., traveling only from parent nodes to child nodes). |
| 13 | + |
| 14 | +### Example 1: |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +``` |
| 19 | +Input: root = [10,5,-3,3,2,null,11,3,-2,null,1], targetSum = 8 |
| 20 | +Output: 3 |
| 21 | +Explanation: The paths that sum to 8 are shown. |
| 22 | +``` |
| 23 | +
|
| 24 | +### Example 2: |
| 25 | +
|
| 26 | +``` |
| 27 | +Input: root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 |
| 28 | +Output: 3 |
| 29 | +``` |
| 30 | +
|
| 31 | +### Constraints: |
| 32 | +
|
| 33 | +- The number of nodes in the tree is in the range `[0, 1000]`. |
| 34 | +- `-10⁹ <= Node.val <= 10⁹` |
| 35 | +- `-1000 <= targetSum <= 1000` |
| 36 | +
|
| 37 | +--- |
| 38 | +
|
| 39 | +## Solutions |
| 40 | +
|
| 41 | +### Solution: Prefix Sum + DFS |
| 42 | +
|
| 43 | +We traverse the tree using depth-first search while maintaining a prefix sum and storing its frequency in a hash map. If at any point the difference between the current prefix sum and `targetSum` exists in the map, it means there is a path that sums up to the target. |
| 44 | +
|
| 45 | +Time complexity: `O(n)` |
| 46 | +Space complexity: `O(n)` |
| 47 | +Where `n` is the number of nodes in the binary tree. |
| 48 | +
|
| 49 | +--- |
| 50 | +
|
| 51 | +### Python3 |
| 52 | +
|
| 53 | +```python |
| 54 | +# Definition for a binary tree node. |
| 55 | +# class TreeNode: |
| 56 | +# def __init__(self, val=0, left=None, right=None): |
| 57 | +# self.val = val |
| 58 | +# self.left = left |
| 59 | +# self.right = right |
| 60 | +from collections import Counter |
| 61 | +
|
| 62 | +class Solution: |
| 63 | + def pathSum(self, root: Optional[TreeNode], targetSum: int) -> int: |
| 64 | + def dfs(node, s): |
| 65 | + if not node: |
| 66 | + return 0 |
| 67 | + s += node.val |
| 68 | + ans = cnt[s - targetSum] |
| 69 | + cnt[s] += 1 |
| 70 | + ans += dfs(node.left, s) |
| 71 | + ans += dfs(node.right, s) |
| 72 | + cnt[s] -= 1 |
| 73 | + return ans |
| 74 | +
|
| 75 | + cnt = Counter({0: 1}) |
| 76 | + return dfs(root, 0) |
| 77 | +``` |
| 78 | + |
| 79 | +### Java |
| 80 | + |
| 81 | +```java |
| 82 | +class Solution { |
| 83 | + private Map<Long, Integer> cnt = new HashMap<>(); |
| 84 | + private int targetSum; |
| 85 | + |
| 86 | + public int pathSum(TreeNode root, int targetSum) { |
| 87 | + cnt.put(0L, 1); |
| 88 | + this.targetSum = targetSum; |
| 89 | + return dfs(root, 0); |
| 90 | + } |
| 91 | + |
| 92 | + private int dfs(TreeNode node, long s) { |
| 93 | + if (node == null) return 0; |
| 94 | + s += node.val; |
| 95 | + int ans = cnt.getOrDefault(s - targetSum, 0); |
| 96 | + cnt.put(s, cnt.getOrDefault(s, 0) + 1); |
| 97 | + ans += dfs(node.left, s) + dfs(node.right, s); |
| 98 | + cnt.put(s, cnt.get(s) - 1); |
| 99 | + return ans; |
| 100 | + } |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +### C++ |
| 105 | + |
| 106 | +```cpp |
| 107 | +class Solution { |
| 108 | +public: |
| 109 | + int pathSum(TreeNode* root, int targetSum) { |
| 110 | + unordered_map<long long, int> cnt; |
| 111 | + cnt[0] = 1; |
| 112 | + return dfs(root, 0, targetSum, cnt); |
| 113 | + } |
| 114 | + |
| 115 | + int dfs(TreeNode* node, long long s, int targetSum, unordered_map<long long, int>& cnt) { |
| 116 | + if (!node) return 0; |
| 117 | + s += node->val; |
| 118 | + int ans = cnt[s - targetSum]; |
| 119 | + cnt[s]++; |
| 120 | + ans += dfs(node->left, s, targetSum, cnt); |
| 121 | + ans += dfs(node->right, s, targetSum, cnt); |
| 122 | + cnt[s]--; |
| 123 | + return ans; |
| 124 | + } |
| 125 | +}; |
| 126 | +``` |
| 127 | + |
| 128 | +### JavaScript |
| 129 | + |
| 130 | +```js |
| 131 | +var pathSum = function (root, targetSum) { |
| 132 | + const cnt = new Map(); |
| 133 | + const dfs = (node, s) => { |
| 134 | + if (!node) return 0; |
| 135 | + s += node.val; |
| 136 | + let ans = cnt.get(s - targetSum) || 0; |
| 137 | + cnt.set(s, (cnt.get(s) || 0) + 1); |
| 138 | + ans += dfs(node.left, s) + dfs(node.right, s); |
| 139 | + cnt.set(s, cnt.get(s) - 1); |
| 140 | + return ans; |
| 141 | + }; |
| 142 | + cnt.set(0, 1); |
| 143 | + return dfs(root, 0); |
| 144 | +}; |
| 145 | +``` |
| 146 | + |
| 147 | +### TypeScript |
| 148 | + |
| 149 | +```ts |
| 150 | +function pathSum(root: TreeNode | null, targetSum: number): number { |
| 151 | + const cnt: Map<number, number> = new Map(); |
| 152 | + const dfs = (node: TreeNode | null, s: number): number => { |
| 153 | + if (!node) return 0; |
| 154 | + s += node.val; |
| 155 | + let ans = cnt.get(s - targetSum) ?? 0; |
| 156 | + cnt.set(s, (cnt.get(s) ?? 0) + 1); |
| 157 | + ans += dfs(node.left, s) + dfs(node.right, s); |
| 158 | + cnt.set(s, (cnt.get(s) ?? 0) - 1); |
| 159 | + return ans; |
| 160 | + }; |
| 161 | + cnt.set(0, 1); |
| 162 | + return dfs(root, 0); |
| 163 | +} |
| 164 | +``` |
| 165 | + |
| 166 | +### Go |
| 167 | + |
| 168 | +```go |
| 169 | +func pathSum(root *TreeNode, targetSum int) int { |
| 170 | + cnt := map[int]int{0: 1} |
| 171 | + var dfs func(*TreeNode, int) int |
| 172 | + dfs = func(node *TreeNode, s int) int { |
| 173 | + if node == nil { |
| 174 | + return 0 |
| 175 | + } |
| 176 | + s += node.Val |
| 177 | + ans := cnt[s-targetSum] |
| 178 | + cnt[s]++ |
| 179 | + ans += dfs(node.Left, s) + dfs(node.Right, s) |
| 180 | + cnt[s]-- |
| 181 | + return ans |
| 182 | + } |
| 183 | + return dfs(root, 0) |
| 184 | +} |
| 185 | +``` |
| 186 | + |
| 187 | +### C# |
| 188 | + |
| 189 | +```cs |
| 190 | +public class Solution { |
| 191 | + public int PathSum(TreeNode root, int targetSum) { |
| 192 | + Dictionary<long, int> cnt = new Dictionary<long, int>(); |
| 193 | + |
| 194 | + int Dfs(TreeNode node, long s) { |
| 195 | + if (node == null) return 0; |
| 196 | + s += node.val; |
| 197 | + int ans = cnt.GetValueOrDefault(s - targetSum, 0); |
| 198 | + cnt[s] = cnt.GetValueOrDefault(s, 0) + 1; |
| 199 | + ans += Dfs(node.left, s); |
| 200 | + ans += Dfs(node.right, s); |
| 201 | + cnt[s]--; |
| 202 | + return ans; |
| 203 | + } |
| 204 | + |
| 205 | + cnt[0] = 1; |
| 206 | + return Dfs(root, 0); |
| 207 | + } |
| 208 | +} |
| 209 | +``` |
| 210 | + |
| 211 | +### Rust |
| 212 | + |
| 213 | +```rust |
| 214 | +use std::rc::Rc; |
| 215 | +use std::cell::RefCell; |
| 216 | +use std::collections::HashMap; |
| 217 | + |
| 218 | +impl Solution { |
| 219 | + pub fn path_sum(root: Option<Rc<RefCell<TreeNode>>>, target_sum: i32) -> i32 { |
| 220 | + let mut cnt = HashMap::new(); |
| 221 | + cnt.insert(0, 1); |
| 222 | + |
| 223 | + fn dfs(node: Option<Rc<RefCell<TreeNode>>>, s: i64, target: i64, cnt: &mut HashMap<i64, i32>) -> i32 { |
| 224 | + if let Some(n) = node { |
| 225 | + let n = n.borrow(); |
| 226 | + let s = s + n.val as i64; |
| 227 | + let ans = cnt.get(&(s - target)).copied().unwrap_or(0); |
| 228 | + *cnt.entry(s).or_insert(0) += 1; |
| 229 | + let ans = ans + dfs(n.left.clone(), s, target, cnt) + dfs(n.right.clone(), s, target, cnt); |
| 230 | + *cnt.get_mut(&s).unwrap() -= 1; |
| 231 | + ans |
| 232 | + } else { |
| 233 | + 0 |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + dfs(root, 0, target_sum as i64, &mut cnt) |
| 238 | + } |
| 239 | +} |
| 240 | +``` |
| 241 | + |
| 242 | +--- |
| 243 | + |
| 244 | +**Tags:** Tree, Depth-First Search, Binary Tree |
| 245 | +**Difficulty:** Medium |
| 246 | +**Related Topics:** Prefix Sum, Recursion |
| 247 | +**Link:** [437. Path Sum III](https://leetcode.com/problems/path-sum-iii) |
| 248 | +``` |
| 249 | +
|
| 250 | +Let me know if you want the Chinese version, or a `.md` file to download. |
0 commit comments