@@ -83,15 +83,13 @@ The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is
8383``` python
8484# Definition for a binary tree node.
8585# class TreeNode:
86- # def __init__(self, x):
87- # self.val = x
88- # self.left = None
89- # self.right = None
90-
91-
86+ # def __init__(self, val=0, left=None, right=None):
87+ # self.val = val
88+ # self.left = left
89+ # self.right = right
9290class Solution :
93- def pathSum (self , root : TreeNode, sum : int ) -> int :
94- def dfs (root : TreeNode, s : int ):
91+ def pathSum (self , root : Optional[ TreeNode] , sum : int ) -> int :
92+ def dfs (root : Optional[ TreeNode] , s : int ) -> int :
9593 if root is None :
9694 return 0
9795 s += root.val
@@ -158,9 +156,8 @@ class Solution {
158156class Solution {
159157public:
160158 int pathSum(TreeNode* root, int sum) {
161- unordered_map<long long, int> cnt;
162- cnt[ 0] = 1;
163- function<int(TreeNode* , long long)> dfs = [ &] (TreeNode* root, long long s) {
159+ unordered_map<long long, int> cnt{{0, 1}};
160+ auto dfs = [ &] (this auto&& dfs, TreeNode* root, long long s) -> int {
164161 if (!root) {
165162 return 0;
166163 }
@@ -298,42 +295,40 @@ impl Solution {
298295#### Swift
299296
300297``` swift
301- /* class TreeNode {
302- * var val: Int
303- * var left: TreeNode?
304- * var right: TreeNode?
305- *
306- * init(_ val: Int, _ left: TreeNode? = nil, _ right: TreeNode? = nil) {
307- * self.val = val
308- * self.left = left
309- * self.right = right
310- * }
311- * }
312- */
313-
298+ /**
299+ * Definition for a binary tree node.
300+ * public class TreeNode {
301+ * public var val: Int
302+ * public var left: TreeNode?
303+ * public var right: TreeNode?
304+ * public init() { self.val = 0; self.left = nil; self.right = nil; }
305+ * public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
306+ * public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
307+ * self.val = val
308+ * self.left = left
309+ * self.right = right
310+ * }
311+ * }
312+ */
314313class Solution {
315- private var cnt: [Int : Int ] = [: ]
316- private var target: Int = 0
317-
318314 func pathSum (_ root : TreeNode? , _ sum : Int ) -> Int {
319- cnt[0 ] = 1
320- target = sum
321- return dfs (root, 0 )
322- }
315+ var cnt: [Int : Int ] = [0 : 1 ]
323316
324- private func dfs (_ root : TreeNode? , _ s : Int ) -> Int {
325- guard let root = root else {
326- return 0
327- }
328- let newSum = s + root.val
329- let ans = cnt[newSum - target, default : 0 ]
317+ func dfs (_ root : TreeNode? , _ s : Int ) -> Int {
318+ guard let root = root else { return 0 }
319+
320+ var s = s + root.val
321+ var ans = cnt[s - sum, default : 0 ]
322+
323+ cnt[s, default : 0 ] += 1
324+ ans += dfs (root.left , s)
325+ ans += dfs (root.right , s)
326+ cnt[s, default : 0 ] -= 1
330327
331- cnt[newSum, default : 0 ] += 1
332- let leftPaths = dfs (root.left , newSum)
333- let rightPaths = dfs (root.right , newSum)
334- cnt[newSum, default : 0 ] -= 1
328+ return ans
329+ }
335330
336- return ans + leftPaths + rightPaths
331+ return dfs (root, 0 )
337332 }
338333}
339334```
0 commit comments