Skip to content

Commit c0ba9fa

Browse files
committed
feat: add solutions to lc problem: No.0113
No.0113.Path Sum II
1 parent 74e30de commit c0ba9fa

File tree

6 files changed

+354
-125
lines changed

6 files changed

+354
-125
lines changed

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

+124-37
Original file line numberDiff line numberDiff line change
@@ -62,28 +62,30 @@
6262
```python
6363
# Definition for a binary tree node.
6464
# class TreeNode:
65-
# def __init__(self, x):
66-
# self.val = x
67-
# self.left = None
68-
# self.right = None
69-
65+
# def __init__(self, val=0, left=None, right=None):
66+
# self.val = val
67+
# self.left = left
68+
# self.right = right
7069
class Solution:
71-
def pathSum(self, root: TreeNode, sum: int) -> List[List[int]]:
72-
def dfs(root, sum):
70+
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
71+
def dfs(root, s):
7372
if root is None:
7473
return
75-
path.append(root.val)
76-
if root.val == sum and root.left is None and root.right is None:
77-
res.append(path.copy())
78-
dfs(root.left, sum - root.val)
79-
dfs(root.right, sum - root.val)
80-
path.pop()
81-
if not root:
82-
return []
83-
res = []
84-
path = []
85-
dfs(root, sum)
86-
return res
74+
t.append(root.val)
75+
s += root.val
76+
if root.left is None and root.right is None:
77+
if s == targetSum:
78+
ans.append(t[:])
79+
dfs(root.left, s)
80+
dfs(root.right, s)
81+
t.pop()
82+
83+
ans = []
84+
t = []
85+
if root is None:
86+
return ans
87+
dfs(root, 0)
88+
return ans
8789
```
8890

8991
### **Java**
@@ -97,34 +99,119 @@ class Solution:
9799
* int val;
98100
* TreeNode left;
99101
* TreeNode right;
100-
* TreeNode(int x) { val = x; }
102+
* TreeNode() {}
103+
* TreeNode(int val) { this.val = val; }
104+
* TreeNode(int val, TreeNode left, TreeNode right) {
105+
* this.val = val;
106+
* this.left = left;
107+
* this.right = right;
108+
* }
101109
* }
102110
*/
103111
class Solution {
104-
private List<List<Integer>> res;
105-
private List<Integer> path;
106-
107-
public List<List<Integer>> pathSum(TreeNode root, int sum) {
108-
if (root == null) return Collections.emptyList();
109-
res = new ArrayList<>();
110-
path = new ArrayList<>();
111-
dfs(root, sum);
112-
return res;
112+
private List<List<Integer>> ans;
113+
private List<Integer> t;
114+
private int target;
115+
116+
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
117+
ans = new ArrayList<>();
118+
t = new ArrayList<>();
119+
target = targetSum;
120+
dfs(root, 0);
121+
return ans;
113122
}
114123

115-
private void dfs(TreeNode root, int sum) {
116-
if (root == null) return;
117-
path.add(root.val);
118-
if (root.val == sum && root.left == null && root.right == null) {
119-
res.add(new ArrayList<>(path));
124+
private void dfs(TreeNode root, int s) {
125+
if (root == null) {
126+
return;
127+
}
128+
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+
}
120134
}
121-
dfs(root.left, sum - root.val);
122-
dfs(root.right, sum - root.val);
123-
path.remove(path.size() - 1);
135+
dfs(root.left, s);
136+
dfs(root.right, s);
137+
t.remove(t.size() - 1);
124138
}
125139
}
126140
```
127141

142+
### **C++**
143+
144+
```cpp
145+
/**
146+
* Definition for a binary tree node.
147+
* struct TreeNode {
148+
* int val;
149+
* TreeNode *left;
150+
* TreeNode *right;
151+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
152+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
153+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
154+
* };
155+
*/
156+
class Solution {
157+
public:
158+
vector<vector<int>> ans;
159+
vector<int> t;
160+
int target;
161+
162+
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
163+
target = targetSum;
164+
dfs(root, 0);
165+
return ans;
166+
}
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+
}
177+
};
178+
```
179+
180+
### **Go**
181+
182+
```go
183+
/**
184+
* Definition for a binary tree node.
185+
* type TreeNode struct {
186+
* Val int
187+
* Left *TreeNode
188+
* Right *TreeNode
189+
* }
190+
*/
191+
func pathSum(root *TreeNode, targetSum int) [][]int {
192+
ans := [][]int{}
193+
t := []int{}
194+
var dfs func(root *TreeNode, s int)
195+
dfs = func(root *TreeNode, s int) {
196+
if root == nil {
197+
return
198+
}
199+
t = append(t, root.Val)
200+
s += root.Val
201+
if root.Left == nil && root.Right == nil && s == targetSum {
202+
cp := make([]int, len(t))
203+
copy(cp, t)
204+
ans = append(ans, cp)
205+
}
206+
dfs(root.Left, s)
207+
dfs(root.Right, s)
208+
t = t[:len(t)-1]
209+
}
210+
dfs(root, 0)
211+
return ans
212+
}
213+
```
214+
128215
### **...**
129216

130217
```

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

+124-37
Original file line numberDiff line numberDiff line change
@@ -48,28 +48,30 @@
4848
```python
4949
# Definition for a binary tree node.
5050
# class TreeNode:
51-
# def __init__(self, x):
52-
# self.val = x
53-
# self.left = None
54-
# self.right = None
55-
51+
# def __init__(self, val=0, left=None, right=None):
52+
# self.val = val
53+
# self.left = left
54+
# self.right = right
5655
class Solution:
57-
def pathSum(self, root: TreeNode, sum: int) -> List[List[int]]:
58-
def dfs(root, sum):
56+
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
57+
def dfs(root, s):
5958
if root is None:
6059
return
61-
path.append(root.val)
62-
if root.val == sum and root.left is None and root.right is None:
63-
res.append(path.copy())
64-
dfs(root.left, sum - root.val)
65-
dfs(root.right, sum - root.val)
66-
path.pop()
67-
if not root:
68-
return []
69-
res = []
70-
path = []
71-
dfs(root, sum)
72-
return res
60+
t.append(root.val)
61+
s += root.val
62+
if root.left is None and root.right is None:
63+
if s == targetSum:
64+
ans.append(t[:])
65+
dfs(root.left, s)
66+
dfs(root.right, s)
67+
t.pop()
68+
69+
ans = []
70+
t = []
71+
if root is None:
72+
return ans
73+
dfs(root, 0)
74+
return ans
7375
```
7476

7577
### **Java**
@@ -81,34 +83,119 @@ class Solution:
8183
* int val;
8284
* TreeNode left;
8385
* TreeNode right;
84-
* TreeNode(int x) { val = x; }
86+
* TreeNode() {}
87+
* TreeNode(int val) { this.val = val; }
88+
* TreeNode(int val, TreeNode left, TreeNode right) {
89+
* this.val = val;
90+
* this.left = left;
91+
* this.right = right;
92+
* }
8593
* }
8694
*/
8795
class Solution {
88-
private List<List<Integer>> res;
89-
private List<Integer> path;
90-
91-
public List<List<Integer>> pathSum(TreeNode root, int sum) {
92-
if (root == null) return Collections.emptyList();
93-
res = new ArrayList<>();
94-
path = new ArrayList<>();
95-
dfs(root, sum);
96-
return res;
96+
private List<List<Integer>> ans;
97+
private List<Integer> t;
98+
private int target;
99+
100+
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
101+
ans = new ArrayList<>();
102+
t = new ArrayList<>();
103+
target = targetSum;
104+
dfs(root, 0);
105+
return ans;
97106
}
98107

99-
private void dfs(TreeNode root, int sum) {
100-
if (root == null) return;
101-
path.add(root.val);
102-
if (root.val == sum && root.left == null && root.right == null) {
103-
res.add(new ArrayList<>(path));
108+
private void dfs(TreeNode root, int s) {
109+
if (root == null) {
110+
return;
111+
}
112+
t.add(root.val);
113+
s += root.val;
114+
if (root.left == null && root.right == null) {
115+
if (s == target) {
116+
ans.add(new ArrayList<>(t));
117+
}
104118
}
105-
dfs(root.left, sum - root.val);
106-
dfs(root.right, sum - root.val);
107-
path.remove(path.size() - 1);
119+
dfs(root.left, s);
120+
dfs(root.right, s);
121+
t.remove(t.size() - 1);
108122
}
109123
}
110124
```
111125

126+
### **C++**
127+
128+
```cpp
129+
/**
130+
* Definition for a binary tree node.
131+
* struct TreeNode {
132+
* int val;
133+
* TreeNode *left;
134+
* TreeNode *right;
135+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
136+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
137+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
138+
* };
139+
*/
140+
class Solution {
141+
public:
142+
vector<vector<int>> ans;
143+
vector<int> t;
144+
int target;
145+
146+
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
147+
target = targetSum;
148+
dfs(root, 0);
149+
return ans;
150+
}
151+
152+
void dfs(TreeNode* root, int s) {
153+
if (!root) return;
154+
t.push_back(root->val);
155+
s += root->val;
156+
if (!root->left && !root->right && s == target) ans.push_back(t);
157+
dfs(root->left, s);
158+
dfs(root->right, s);
159+
t.pop_back();
160+
}
161+
};
162+
```
163+
164+
### **Go**
165+
166+
```go
167+
/**
168+
* Definition for a binary tree node.
169+
* type TreeNode struct {
170+
* Val int
171+
* Left *TreeNode
172+
* Right *TreeNode
173+
* }
174+
*/
175+
func pathSum(root *TreeNode, targetSum int) [][]int {
176+
ans := [][]int{}
177+
t := []int{}
178+
var dfs func(root *TreeNode, s int)
179+
dfs = func(root *TreeNode, s int) {
180+
if root == nil {
181+
return
182+
}
183+
t = append(t, root.Val)
184+
s += root.Val
185+
if root.Left == nil && root.Right == nil && s == targetSum {
186+
cp := make([]int, len(t))
187+
copy(cp, t)
188+
ans = append(ans, cp)
189+
}
190+
dfs(root.Left, s)
191+
dfs(root.Right, s)
192+
t = t[:len(t)-1]
193+
}
194+
dfs(root, 0)
195+
return ans
196+
}
197+
```
198+
112199
### **...**
113200

114201
```

0 commit comments

Comments
 (0)