|
46 | 46 |
|
47 | 47 | <!-- 这里可写通用的实现逻辑 -->
|
48 | 48 |
|
| 49 | +在遍历的过程中,记录当前路径上的前缀和 |
| 50 | + |
49 | 51 | <!-- tabs:start -->
|
50 | 52 |
|
51 | 53 | ### **Python3**
|
52 | 54 |
|
53 | 55 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
54 | 56 |
|
55 | 57 | ```python
|
56 |
| - |
| 58 | +# Definition for a binary tree node. |
| 59 | +# class TreeNode: |
| 60 | +# def __init__(self, val=0, left=None, right=None): |
| 61 | +# self.val = val |
| 62 | +# self.left = left |
| 63 | +# self.right = right |
| 64 | +class Solution: |
| 65 | + def pathSum(self, root: TreeNode, targetSum: int) -> int: |
| 66 | + preSum = defaultdict(int) |
| 67 | + preSum[0] = 1 |
| 68 | + |
| 69 | + def dfs(node: TreeNode, cur: int) -> int: |
| 70 | + if not node: |
| 71 | + return 0 |
| 72 | + |
| 73 | + cur += node.val |
| 74 | + ret = preSum[cur - targetSum] |
| 75 | + |
| 76 | + preSum[cur] += 1 |
| 77 | + ret += dfs(node.left, cur) |
| 78 | + ret += dfs(node.right, cur) |
| 79 | + preSum[cur] -= 1 |
| 80 | + |
| 81 | + return ret |
| 82 | + |
| 83 | + return dfs(root, 0) |
57 | 84 | ```
|
58 | 85 |
|
59 | 86 | ### **Java**
|
60 | 87 |
|
61 | 88 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
62 | 89 |
|
63 | 90 | ```java
|
| 91 | +/** |
| 92 | + * Definition for a binary tree node. |
| 93 | + * public class TreeNode { |
| 94 | + * int val; |
| 95 | + * TreeNode left; |
| 96 | + * TreeNode right; |
| 97 | + * TreeNode() {} |
| 98 | + * TreeNode(int val) { this.val = val; } |
| 99 | + * TreeNode(int val, TreeNode left, TreeNode right) { |
| 100 | + * this.val = val; |
| 101 | + * this.left = left; |
| 102 | + * this.right = right; |
| 103 | + * } |
| 104 | + * } |
| 105 | + */ |
| 106 | +class Solution { |
| 107 | + |
| 108 | + private final Map<Integer, Integer> preSum = new HashMap<>(); |
| 109 | + |
| 110 | + public int pathSum(TreeNode root, int targetSum) { |
| 111 | + preSum.put(0, 1); |
| 112 | + return dfs(root, 0, targetSum); |
| 113 | + } |
| 114 | + |
| 115 | + private int dfs(TreeNode node, int cur, int targetSum) { |
| 116 | + if (node == null) { |
| 117 | + return 0; |
| 118 | + } |
| 119 | + |
| 120 | + cur += node.val; |
| 121 | + int ret = preSum.getOrDefault(cur - targetSum, 0); |
| 122 | + |
| 123 | + preSum.merge(cur, 1, Integer::sum); |
| 124 | + ret += dfs(node.left, cur, targetSum); |
| 125 | + ret += dfs(node.right, cur, targetSum); |
| 126 | + preSum.merge(cur, -1, Integer::sum); |
| 127 | + |
| 128 | + return ret; |
| 129 | + } |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +### **Go** |
| 134 | + |
| 135 | +```go |
| 136 | +/** |
| 137 | + * Definition for a binary tree node. |
| 138 | + * type TreeNode struct { |
| 139 | + * Val int |
| 140 | + * Left *TreeNode |
| 141 | + * Right *TreeNode |
| 142 | + * } |
| 143 | + */ |
| 144 | +func pathSum(root *TreeNode, targetSum int) int { |
| 145 | + preSum := make(map[int]int) |
| 146 | + preSum[0] = 1 |
| 147 | + |
| 148 | + var dfs func(*TreeNode, int) int |
| 149 | + dfs = func(node *TreeNode, cur int) int { |
| 150 | + if node == nil { |
| 151 | + return 0 |
| 152 | + } |
| 153 | + |
| 154 | + cur += node.Val |
| 155 | + ret := preSum[cur-targetSum] |
| 156 | + |
| 157 | + preSum[cur]++ |
| 158 | + ret += dfs(node.Left, cur) |
| 159 | + ret += dfs(node.Right, cur) |
| 160 | + preSum[cur]-- |
| 161 | + |
| 162 | + return ret |
| 163 | + } |
| 164 | + |
| 165 | + return dfs(root, 0) |
| 166 | +} |
| 167 | +``` |
64 | 168 |
|
| 169 | +### **C++** |
| 170 | + |
| 171 | +```cpp |
| 172 | +/** |
| 173 | + * Definition for a binary tree node. |
| 174 | + * struct TreeNode { |
| 175 | + * int val; |
| 176 | + * TreeNode *left; |
| 177 | + * TreeNode *right; |
| 178 | + * TreeNode() : val(0), left(nullptr), right(nullptr) {} |
| 179 | + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} |
| 180 | + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} |
| 181 | + * }; |
| 182 | + */ |
| 183 | +class Solution { |
| 184 | +public: |
| 185 | + int pathSum(TreeNode* root, int targetSum) { |
| 186 | + unordered_map<int, int> preSum; |
| 187 | + preSum[0] = 1; |
| 188 | + |
| 189 | + function<int(TreeNode*, int)> dfs = [&](TreeNode* node, int cur) { |
| 190 | + if (node == nullptr) { |
| 191 | + return 0; |
| 192 | + } |
| 193 | + |
| 194 | + cur += node->val; |
| 195 | + int ret = preSum[cur - targetSum]; |
| 196 | + |
| 197 | + ++preSum[cur]; |
| 198 | + ret += dfs(node->left, cur); |
| 199 | + ret += dfs(node->right, cur); |
| 200 | + --preSum[cur]; |
| 201 | + |
| 202 | + return ret; |
| 203 | + }; |
| 204 | + |
| 205 | + return dfs(root, 0); |
| 206 | + } |
| 207 | +}; |
65 | 208 | ```
|
66 | 209 |
|
67 | 210 | ### **...**
|
|
0 commit comments