Skip to content

Commit 4ec6714

Browse files
committed
feat: add solutions to lc problem: No.0113
No.0113.Path Sum II
1 parent 3c1d6e3 commit 4ec6714

File tree

7 files changed

+185
-128
lines changed

7 files changed

+185
-128
lines changed

solution/0100-0199/0113.Path Sum II/README.md

+65-42
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@
5151

5252
<!-- 这里可写通用的实现逻辑 -->
5353

54-
深度优先搜索+路径记录。
54+
**方法一:DFS**
55+
56+
我们从根节点开始,递归遍历所有从根节点到叶子节点的路径,并记录路径和。当遍历到叶子节点时,如果此时路径和等于 `targetSum`,则将此路径加入答案。
57+
58+
时间复杂度 $O(n^2)$,其中 $n$ 是二叉树的节点数。空间复杂度 $O(n)$。
5559

5660
<!-- tabs:start -->
5761

@@ -71,19 +75,16 @@ class Solution:
7175
def dfs(root, s):
7276
if root is None:
7377
return
74-
t.append(root.val)
7578
s += root.val
76-
if root.left is None and root.right is None:
77-
if s == targetSum:
78-
ans.append(t[:])
79+
t.append(root.val)
80+
if root.left is None and root.right is None and s == targetSum:
81+
ans.append(t[:])
7982
dfs(root.left, s)
8083
dfs(root.right, s)
8184
t.pop()
8285

8386
ans = []
8487
t = []
85-
if root is None:
86-
return ans
8788
dfs(root, 0)
8889
return ans
8990
```
@@ -109,28 +110,22 @@ class Solution:
109110
* }
110111
*/
111112
class Solution {
112-
private List<List<Integer>> ans;
113-
private List<Integer> t;
114-
private int target;
113+
private List<List<Integer>> ans = new ArrayList<>();
114+
private List<Integer> t = new ArrayList<>();
115115

116116
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
117-
ans = new ArrayList<>();
118-
t = new ArrayList<>();
119-
target = targetSum;
120-
dfs(root, 0);
117+
dfs(root, targetSum);
121118
return ans;
122119
}
123120

124121
private void dfs(TreeNode root, int s) {
125122
if (root == null) {
126123
return;
127124
}
125+
s -= root.val;
128126
t.add(root.val);
129-
s += root.val;
130-
if (root.left == null && root.right == null) {
131-
if (s == target) {
132-
ans.add(new ArrayList<>(t));
133-
}
127+
if (root.left == null && root.right == null && s == 0) {
128+
ans.add(new ArrayList<>(t));
134129
}
135130
dfs(root.left, s);
136131
dfs(root.right, s);
@@ -155,25 +150,21 @@ class Solution {
155150
*/
156151
class Solution {
157152
public:
158-
vector<vector<int>> ans;
159-
vector<int> t;
160-
int target;
161-
162153
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
163-
target = targetSum;
164-
dfs(root, 0);
154+
vector<vector<int>> ans;
155+
vector<int> t;
156+
function<void(TreeNode*, int)> dfs = [&](TreeNode* root, int s) {
157+
if (!root) return;
158+
s -= root->val;
159+
t.emplace_back(root->val);
160+
if (!root->left && !root->right && s == 0) ans.emplace_back(t);
161+
dfs(root->left, s);
162+
dfs(root->right, s);
163+
t.pop_back();
164+
};
165+
dfs(root, targetSum);
165166
return ans;
166167
}
167-
168-
void dfs(TreeNode* root, int s) {
169-
if (!root) return;
170-
t.push_back(root->val);
171-
s += root->val;
172-
if (!root->left && !root->right && s == target) ans.push_back(t);
173-
dfs(root->left, s);
174-
dfs(root->right, s);
175-
t.pop_back();
176-
}
177168
};
178169
```
179170
@@ -188,17 +179,16 @@ public:
188179
* Right *TreeNode
189180
* }
190181
*/
191-
func pathSum(root *TreeNode, targetSum int) [][]int {
192-
ans := [][]int{}
182+
func pathSum(root *TreeNode, targetSum int) (ans [][]int) {
193183
t := []int{}
194-
var dfs func(root *TreeNode, s int)
184+
var dfs func(*TreeNode, int)
195185
dfs = func(root *TreeNode, s int) {
196186
if root == nil {
197187
return
198188
}
189+
s -= root.Val
199190
t = append(t, root.Val)
200-
s += root.Val
201-
if root.Left == nil && root.Right == nil && s == targetSum {
191+
if root.Left == nil && root.Right == nil && s == 0 {
202192
cp := make([]int, len(t))
203193
copy(cp, t)
204194
ans = append(ans, cp)
@@ -207,8 +197,8 @@ func pathSum(root *TreeNode, targetSum int) [][]int {
207197
dfs(root.Right, s)
208198
t = t[:len(t)-1]
209199
}
210-
dfs(root, 0)
211-
return ans
200+
dfs(root, targetSum)
201+
return
212202
}
213203
```
214204

@@ -271,6 +261,39 @@ impl Solution {
271261
}
272262
```
273263

264+
### **JavaScript**
265+
266+
```js
267+
/**
268+
* Definition for a binary tree node.
269+
* function TreeNode(val, left, right) {
270+
* this.val = (val===undefined ? 0 : val)
271+
* this.left = (left===undefined ? null : left)
272+
* this.right = (right===undefined ? null : right)
273+
* }
274+
*/
275+
/**
276+
* @param {TreeNode} root
277+
* @param {number} targetSum
278+
* @return {number[][]}
279+
*/
280+
var pathSum = function (root, targetSum) {
281+
const ans = [];
282+
const t = [];
283+
function dfs(root, s) {
284+
if (!root) return;
285+
s -= root.val;
286+
t.push(root.val);
287+
if (!root.left && !root.right && s == 0) ans.push([...t]);
288+
dfs(root.left, s);
289+
dfs(root.right, s);
290+
t.pop();
291+
}
292+
dfs(root, targetSum);
293+
return ans;
294+
};
295+
```
296+
274297
### **...**
275298

276299
```

solution/0100-0199/0113.Path Sum II/README_EN.md

+60-41
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,16 @@ class Solution:
6060
def dfs(root, s):
6161
if root is None:
6262
return
63-
t.append(root.val)
6463
s += root.val
65-
if root.left is None and root.right is None:
66-
if s == targetSum:
67-
ans.append(t[:])
64+
t.append(root.val)
65+
if root.left is None and root.right is None and s == targetSum:
66+
ans.append(t[:])
6867
dfs(root.left, s)
6968
dfs(root.right, s)
7069
t.pop()
7170

7271
ans = []
7372
t = []
74-
if root is None:
75-
return ans
7673
dfs(root, 0)
7774
return ans
7875
```
@@ -96,28 +93,22 @@ class Solution:
9693
* }
9794
*/
9895
class Solution {
99-
private List<List<Integer>> ans;
100-
private List<Integer> t;
101-
private int target;
96+
private List<List<Integer>> ans = new ArrayList<>();
97+
private List<Integer> t = new ArrayList<>();
10298

10399
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
104-
ans = new ArrayList<>();
105-
t = new ArrayList<>();
106-
target = targetSum;
107-
dfs(root, 0);
100+
dfs(root, targetSum);
108101
return ans;
109102
}
110103

111104
private void dfs(TreeNode root, int s) {
112105
if (root == null) {
113106
return;
114107
}
108+
s -= root.val;
115109
t.add(root.val);
116-
s += root.val;
117-
if (root.left == null && root.right == null) {
118-
if (s == target) {
119-
ans.add(new ArrayList<>(t));
120-
}
110+
if (root.left == null && root.right == null && s == 0) {
111+
ans.add(new ArrayList<>(t));
121112
}
122113
dfs(root.left, s);
123114
dfs(root.right, s);
@@ -142,25 +133,21 @@ class Solution {
142133
*/
143134
class Solution {
144135
public:
145-
vector<vector<int>> ans;
146-
vector<int> t;
147-
int target;
148-
149136
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
150-
target = targetSum;
151-
dfs(root, 0);
137+
vector<vector<int>> ans;
138+
vector<int> t;
139+
function<void(TreeNode*, int)> dfs = [&](TreeNode* root, int s) {
140+
if (!root) return;
141+
s -= root->val;
142+
t.emplace_back(root->val);
143+
if (!root->left && !root->right && s == 0) ans.emplace_back(t);
144+
dfs(root->left, s);
145+
dfs(root->right, s);
146+
t.pop_back();
147+
};
148+
dfs(root, targetSum);
152149
return ans;
153150
}
154-
155-
void dfs(TreeNode* root, int s) {
156-
if (!root) return;
157-
t.push_back(root->val);
158-
s += root->val;
159-
if (!root->left && !root->right && s == target) ans.push_back(t);
160-
dfs(root->left, s);
161-
dfs(root->right, s);
162-
t.pop_back();
163-
}
164151
};
165152
```
166153
@@ -175,17 +162,16 @@ public:
175162
* Right *TreeNode
176163
* }
177164
*/
178-
func pathSum(root *TreeNode, targetSum int) [][]int {
179-
ans := [][]int{}
165+
func pathSum(root *TreeNode, targetSum int) (ans [][]int) {
180166
t := []int{}
181-
var dfs func(root *TreeNode, s int)
167+
var dfs func(*TreeNode, int)
182168
dfs = func(root *TreeNode, s int) {
183169
if root == nil {
184170
return
185171
}
172+
s -= root.Val
186173
t = append(t, root.Val)
187-
s += root.Val
188-
if root.Left == nil && root.Right == nil && s == targetSum {
174+
if root.Left == nil && root.Right == nil && s == 0 {
189175
cp := make([]int, len(t))
190176
copy(cp, t)
191177
ans = append(ans, cp)
@@ -194,8 +180,8 @@ func pathSum(root *TreeNode, targetSum int) [][]int {
194180
dfs(root.Right, s)
195181
t = t[:len(t)-1]
196182
}
197-
dfs(root, 0)
198-
return ans
183+
dfs(root, targetSum)
184+
return
199185
}
200186
```
201187

@@ -258,6 +244,39 @@ impl Solution {
258244
}
259245
```
260246

247+
### **JavaScript**
248+
249+
```js
250+
/**
251+
* Definition for a binary tree node.
252+
* function TreeNode(val, left, right) {
253+
* this.val = (val===undefined ? 0 : val)
254+
* this.left = (left===undefined ? null : left)
255+
* this.right = (right===undefined ? null : right)
256+
* }
257+
*/
258+
/**
259+
* @param {TreeNode} root
260+
* @param {number} targetSum
261+
* @return {number[][]}
262+
*/
263+
var pathSum = function (root, targetSum) {
264+
const ans = [];
265+
const t = [];
266+
function dfs(root, s) {
267+
if (!root) return;
268+
s -= root.val;
269+
t.push(root.val);
270+
if (!root.left && !root.right && s == 0) ans.push([...t]);
271+
dfs(root.left, s);
272+
dfs(root.right, s);
273+
t.pop();
274+
}
275+
dfs(root, targetSum);
276+
return ans;
277+
};
278+
```
279+
261280
### **...**
262281

263282
```

solution/0100-0199/0113.Path Sum II/Solution.cpp

+12-16
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,19 @@
1111
*/
1212
class Solution {
1313
public:
14-
vector<vector<int>> ans;
15-
vector<int> t;
16-
int target;
17-
1814
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
19-
target = targetSum;
20-
dfs(root, 0);
15+
vector<vector<int>> ans;
16+
vector<int> t;
17+
function<void(TreeNode*, int)> dfs = [&](TreeNode* root, int s) {
18+
if (!root) return;
19+
s -= root->val;
20+
t.emplace_back(root->val);
21+
if (!root->left && !root->right && s == 0) ans.emplace_back(t);
22+
dfs(root->left, s);
23+
dfs(root->right, s);
24+
t.pop_back();
25+
};
26+
dfs(root, targetSum);
2127
return ans;
2228
}
23-
24-
void dfs(TreeNode* root, int s) {
25-
if (!root) return;
26-
t.push_back(root->val);
27-
s += root->val;
28-
if (!root->left && !root->right && s == target) ans.push_back(t);
29-
dfs(root->left, s);
30-
dfs(root->right, s);
31-
t.pop_back();
32-
}
3329
};

0 commit comments

Comments
 (0)