File tree Expand file tree Collapse file tree 3 files changed +121
-0
lines changed
lcci/04.12.Paths with Sum Expand file tree Collapse file tree 3 files changed +121
-0
lines changed Original file line number Diff line number Diff line change @@ -257,6 +257,48 @@ impl Solution {
257
257
}
258
258
```
259
259
260
+ ``` swift
261
+ /* class TreeNode {
262
+ * var val: Int
263
+ * var left: TreeNode?
264
+ * var right: TreeNode?
265
+ *
266
+ * init(_ val: Int, _ left: TreeNode? = nil, _ right: TreeNode? = nil) {
267
+ * self.val = val
268
+ * self.left = left
269
+ * self.right = right
270
+ * }
271
+ * }
272
+ */
273
+
274
+ class Solution {
275
+ private var cnt: [Int : Int ] = [: ]
276
+ private var target: Int = 0
277
+
278
+ func pathSum (_ root : TreeNode? , _ sum : Int ) -> Int {
279
+ cnt[0 ] = 1
280
+ target = sum
281
+ return dfs (root, 0 )
282
+
283
+ }
284
+
285
+ private func dfs (_ root : TreeNode? , _ s : Int ) -> Int {
286
+ guard let root = root else {
287
+ return 0
288
+ }
289
+ let newSum = s + root.val
290
+ let ans = cnt[newSum - target, default : 0 ]
291
+
292
+ cnt[newSum, default : 0 ] += 1
293
+ let leftPaths = dfs (root.left , newSum)
294
+ let rightPaths = dfs (root.right , newSum)
295
+ cnt[newSum, default : 0 ] -= 1
296
+
297
+ return ans + leftPaths + rightPaths
298
+ }
299
+ }
300
+ ```
301
+
260
302
<!-- tabs: end -->
261
303
262
304
<!-- end -->
Original file line number Diff line number Diff line change @@ -269,6 +269,47 @@ impl Solution {
269
269
}
270
270
```
271
271
272
+ ``` swift
273
+ /* class TreeNode {
274
+ * var val: Int
275
+ * var left: TreeNode?
276
+ * var right: TreeNode?
277
+ *
278
+ * init(_ val: Int, _ left: TreeNode? = nil, _ right: TreeNode? = nil) {
279
+ * self.val = val
280
+ * self.left = left
281
+ * self.right = right
282
+ * }
283
+ * }
284
+ */
285
+
286
+ class Solution {
287
+ private var cnt: [Int : Int ] = [: ]
288
+ private var target: Int = 0
289
+
290
+ func pathSum (_ root : TreeNode? , _ sum : Int ) -> Int {
291
+ cnt[0 ] = 1
292
+ target = sum
293
+ return dfs (root, 0 )
294
+ }
295
+
296
+ private func dfs (_ root : TreeNode? , _ s : Int ) -> Int {
297
+ guard let root = root else {
298
+ return 0
299
+ }
300
+ let newSum = s + root.val
301
+ let ans = cnt[newSum - target, default : 0 ]
302
+
303
+ cnt[newSum, default : 0 ] += 1
304
+ let leftPaths = dfs (root.left , newSum)
305
+ let rightPaths = dfs (root.right , newSum)
306
+ cnt[newSum, default : 0 ] -= 1
307
+
308
+ return ans + leftPaths + rightPaths
309
+ }
310
+ }
311
+ ```
312
+
272
313
<!-- tabs: end -->
273
314
274
315
<!-- end -->
Original file line number Diff line number Diff line change
1
+ /* class TreeNode {
2
+ * var val: Int
3
+ * var left: TreeNode?
4
+ * var right: TreeNode?
5
+ *
6
+ * init(_ val: Int, _ left: TreeNode? = nil, _ right: TreeNode? = nil) {
7
+ * self.val = val
8
+ * self.left = left
9
+ * self.right = right
10
+ * }
11
+ * }
12
+ */
13
+
14
+ class Solution {
15
+ private var cnt : [ Int : Int ] = [ : ]
16
+ private var target : Int = 0
17
+
18
+ func pathSum( _ root: TreeNode ? , _ sum: Int ) -> Int {
19
+ cnt [ 0 ] = 1
20
+ target = sum
21
+ return dfs ( root, 0 )
22
+ }
23
+
24
+ private func dfs( _ root: TreeNode ? , _ s: Int ) -> Int {
25
+ guard let root = root else {
26
+ return 0
27
+ }
28
+ let newSum = s + root. val
29
+ let ans = cnt [ newSum - target, default: 0 ]
30
+
31
+ cnt [ newSum, default: 0 ] += 1
32
+ let leftPaths = dfs ( root. left, newSum)
33
+ let rightPaths = dfs ( root. right, newSum)
34
+ cnt [ newSum, default: 0 ] -= 1
35
+
36
+ return ans + leftPaths + rightPaths
37
+ }
38
+ }
You can’t perform that action at this time.
0 commit comments