Skip to content

Commit f241688

Browse files
committed
feat: add solutions to lc problem: No.0112
No.0112.Path Sum
1 parent 7609fe9 commit f241688

File tree

7 files changed

+298
-82
lines changed

7 files changed

+298
-82
lines changed

solution/0100-0199/0112.Path Sum/README.md

+106-28
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@
5252

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

55-
递归求解,递归地询问它的子节点是否能满足条件即可。
55+
**方法一:递归**
56+
57+
从根节点开始,递归地对树进行遍历,并在遍历过程中更新节点的值为从根节点到该节点的路径和。当遍历到叶子节点时,判断该路径和是否等于目标值,如果相等则返回 `true`,否则返回 `false`
58+
59+
时间复杂度 $O(n)$,其中 $n$ 是二叉树的节点数。对每个节点访问一次。
5660

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

@@ -63,22 +67,21 @@
6367
```python
6468
# Definition for a binary tree node.
6569
# class TreeNode:
66-
# def __init__(self, x):
67-
# self.val = x
68-
# self.left = None
69-
# self.right = None
70-
71-
70+
# def __init__(self, val=0, left=None, right=None):
71+
# self.val = val
72+
# self.left = left
73+
# self.right = right
7274
class Solution:
73-
def hasPathSum(self, root: TreeNode, sum: int) -> bool:
74-
def dfs(root, sum):
75+
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
76+
def dfs(root, s):
7577
if root is None:
7678
return False
77-
if root.val == sum and root.left is None and root.right is None:
79+
s += root.val
80+
if root.left is None and root.right is None and s == targetSum:
7881
return True
79-
return dfs(root.left, sum - root.val) or dfs(root.right, sum - root.val)
82+
return dfs(root.left, s) or dfs(root.right, s)
8083

81-
return dfs(root, sum)
84+
return dfs(root, 0)
8285
```
8386

8487
### **Java**
@@ -92,40 +95,88 @@ class Solution:
9295
* int val;
9396
* TreeNode left;
9497
* TreeNode right;
95-
* TreeNode(int x) { val = x; }
98+
* TreeNode() {}
99+
* TreeNode(int val) { this.val = val; }
100+
* TreeNode(int val, TreeNode left, TreeNode right) {
101+
* this.val = val;
102+
* this.left = left;
103+
* this.right = right;
104+
* }
96105
* }
97106
*/
98107
class Solution {
99-
public boolean hasPathSum(TreeNode root, int sum) {
100-
return dfs(root, sum);
108+
public boolean hasPathSum(TreeNode root, int targetSum) {
109+
return dfs(root, targetSum);
101110
}
102111

103-
private boolean dfs(TreeNode root, int sum) {
104-
if (root == null) return false;
105-
if (root.val == sum && root.left == null && root.right == null) return true;
106-
return dfs(root.left, sum - root.val) || dfs(root.right, sum - root.val);
112+
private boolean dfs(TreeNode root, int s) {
113+
if (root == null) {
114+
return false;
115+
}
116+
s -= root.val;
117+
if (root.left == null && root.right == null && s == 0) {
118+
return true;
119+
}
120+
return dfs(root.left, s) || dfs(root.right, s);
107121
}
108122
}
109123
```
110124

111125
### **C++**
112126

113127
```cpp
128+
/**
129+
* Definition for a binary tree node.
130+
* struct TreeNode {
131+
* int val;
132+
* TreeNode *left;
133+
* TreeNode *right;
134+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
135+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
136+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
137+
* };
138+
*/
114139
class Solution {
115140
public:
116-
bool hasPathSum(TreeNode* root, int sum) {
117-
118-
if (root == NULL) return false;
119-
if (root->right == NULL && root->left == NULL && sum == root->val) return true;
120-
121-
bool leftTrue = hasPathSum(root->left, sum - root->val);
122-
bool rightTrue = hasPathSum(root->right, sum - root->val);
123-
124-
return (leftTrue || rightTrue);
141+
bool hasPathSum(TreeNode* root, int targetSum) {
142+
function<bool(TreeNode*, int)> dfs = [&](TreeNode* root, int s) -> int {
143+
if (!root) return false;
144+
s += root->val;
145+
if (!root->left && !root->right && s == targetSum) return true;
146+
return dfs(root->left, s) || dfs(root->right, s);
147+
};
148+
return dfs(root, 0);
125149
}
126150
};
127151
```
128152
153+
### **Go**
154+
155+
```go
156+
/**
157+
* Definition for a binary tree node.
158+
* type TreeNode struct {
159+
* Val int
160+
* Left *TreeNode
161+
* Right *TreeNode
162+
* }
163+
*/
164+
func hasPathSum(root *TreeNode, targetSum int) bool {
165+
var dfs func(*TreeNode, int) bool
166+
dfs = func(root *TreeNode, s int) bool {
167+
if root == nil {
168+
return false
169+
}
170+
s += root.Val
171+
if root.Left == nil && root.Right == nil && s == targetSum {
172+
return true
173+
}
174+
return dfs(root.Left, s) || dfs(root.Right, s)
175+
}
176+
return dfs(root, 0)
177+
}
178+
```
179+
129180
### **TypeScript**
130181

131182
```ts
@@ -199,6 +250,33 @@ impl Solution {
199250
}
200251
```
201252

253+
### **JavaScript**
254+
255+
```js
256+
/**
257+
* Definition for a binary tree node.
258+
* function TreeNode(val, left, right) {
259+
* this.val = (val===undefined ? 0 : val)
260+
* this.left = (left===undefined ? null : left)
261+
* this.right = (right===undefined ? null : right)
262+
* }
263+
*/
264+
/**
265+
* @param {TreeNode} root
266+
* @param {number} targetSum
267+
* @return {boolean}
268+
*/
269+
var hasPathSum = function (root, targetSum) {
270+
function dfs(root, s) {
271+
if (!root) return false;
272+
s += root.val;
273+
if (!root.left && !root.right && s == targetSum) return true;
274+
return dfs(root.left, s) || dfs(root.right, s);
275+
}
276+
return dfs(root, 0);
277+
};
278+
```
279+
202280
### **...**
203281

204282
```

solution/0100-0199/0112.Path Sum/README_EN.md

+101-27
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,21 @@ There is no root-to-leaf path with sum = 5.
5454
```python
5555
# Definition for a binary tree node.
5656
# class TreeNode:
57-
# def __init__(self, x):
58-
# self.val = x
59-
# self.left = None
60-
# self.right = None
61-
62-
57+
# def __init__(self, val=0, left=None, right=None):
58+
# self.val = val
59+
# self.left = left
60+
# self.right = right
6361
class Solution:
64-
def hasPathSum(self, root: TreeNode, sum: int) -> bool:
65-
def dfs(root, sum):
62+
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
63+
def dfs(root, s):
6664
if root is None:
6765
return False
68-
if root.val == sum and root.left is None and root.right is None:
66+
s += root.val
67+
if root.left is None and root.right is None and s == targetSum:
6968
return True
70-
return dfs(root.left, sum - root.val) or dfs(root.right, sum - root.val)
69+
return dfs(root.left, s) or dfs(root.right, s)
7170

72-
return dfs(root, sum)
71+
return dfs(root, 0)
7372
```
7473

7574
### **Java**
@@ -81,40 +80,88 @@ class Solution:
8180
* int val;
8281
* TreeNode left;
8382
* TreeNode right;
84-
* TreeNode(int x) { val = x; }
83+
* TreeNode() {}
84+
* TreeNode(int val) { this.val = val; }
85+
* TreeNode(int val, TreeNode left, TreeNode right) {
86+
* this.val = val;
87+
* this.left = left;
88+
* this.right = right;
89+
* }
8590
* }
8691
*/
8792
class Solution {
88-
public boolean hasPathSum(TreeNode root, int sum) {
89-
return dfs(root, sum);
93+
public boolean hasPathSum(TreeNode root, int targetSum) {
94+
return dfs(root, targetSum);
9095
}
9196

92-
private boolean dfs(TreeNode root, int sum) {
93-
if (root == null) return false;
94-
if (root.val == sum && root.left == null && root.right == null) return true;
95-
return dfs(root.left, sum - root.val) || dfs(root.right, sum - root.val);
97+
private boolean dfs(TreeNode root, int s) {
98+
if (root == null) {
99+
return false;
100+
}
101+
s -= root.val;
102+
if (root.left == null && root.right == null && s == 0) {
103+
return true;
104+
}
105+
return dfs(root.left, s) || dfs(root.right, s);
96106
}
97107
}
98108
```
99109

100110
### **C++**
101111

102112
```cpp
113+
/**
114+
* Definition for a binary tree node.
115+
* struct TreeNode {
116+
* int val;
117+
* TreeNode *left;
118+
* TreeNode *right;
119+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
120+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
121+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
122+
* };
123+
*/
103124
class Solution {
104125
public:
105-
bool hasPathSum(TreeNode* root, int sum) {
106-
107-
if (root == NULL) return false;
108-
if (root->right == NULL && root->left == NULL && sum == root->val) return true;
109-
110-
bool leftTrue = hasPathSum(root->left, sum - root->val);
111-
bool rightTrue = hasPathSum(root->right, sum - root->val);
112-
113-
return (leftTrue || rightTrue);
126+
bool hasPathSum(TreeNode* root, int targetSum) {
127+
function<bool(TreeNode*, int)> dfs = [&](TreeNode* root, int s) -> int {
128+
if (!root) return false;
129+
s += root->val;
130+
if (!root->left && !root->right && s == targetSum) return true;
131+
return dfs(root->left, s) || dfs(root->right, s);
132+
};
133+
return dfs(root, 0);
114134
}
115135
};
116136
```
117137
138+
### **Go**
139+
140+
```go
141+
/**
142+
* Definition for a binary tree node.
143+
* type TreeNode struct {
144+
* Val int
145+
* Left *TreeNode
146+
* Right *TreeNode
147+
* }
148+
*/
149+
func hasPathSum(root *TreeNode, targetSum int) bool {
150+
var dfs func(*TreeNode, int) bool
151+
dfs = func(root *TreeNode, s int) bool {
152+
if root == nil {
153+
return false
154+
}
155+
s += root.Val
156+
if root.Left == nil && root.Right == nil && s == targetSum {
157+
return true
158+
}
159+
return dfs(root.Left, s) || dfs(root.Right, s)
160+
}
161+
return dfs(root, 0)
162+
}
163+
```
164+
118165
### **TypeScript**
119166

120167
```ts
@@ -188,6 +235,33 @@ impl Solution {
188235
}
189236
```
190237

238+
### **JavaScript**
239+
240+
```js
241+
/**
242+
* Definition for a binary tree node.
243+
* function TreeNode(val, left, right) {
244+
* this.val = (val===undefined ? 0 : val)
245+
* this.left = (left===undefined ? null : left)
246+
* this.right = (right===undefined ? null : right)
247+
* }
248+
*/
249+
/**
250+
* @param {TreeNode} root
251+
* @param {number} targetSum
252+
* @return {boolean}
253+
*/
254+
var hasPathSum = function (root, targetSum) {
255+
function dfs(root, s) {
256+
if (!root) return false;
257+
s += root.val;
258+
if (!root.left && !root.right && s == targetSum) return true;
259+
return dfs(root.left, s) || dfs(root.right, s);
260+
}
261+
return dfs(root, 0);
262+
};
263+
```
264+
191265
### **...**
192266

193267
```
+19-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
112
class Solution {
213
public:
3-
bool hasPathSum(TreeNode* root, int sum) {
4-
5-
if (root == NULL) return false;
6-
if (root->right == NULL && root->left == NULL && sum == root->val) return true;
7-
8-
bool leftTrue = hasPathSum(root->left, sum - root->val);
9-
bool rightTrue = hasPathSum(root->right, sum - root->val);
10-
11-
return (leftTrue || rightTrue);
14+
bool hasPathSum(TreeNode* root, int targetSum) {
15+
function<bool(TreeNode*, int)> dfs = [&](TreeNode* root, int s) -> int {
16+
if (!root) return false;
17+
s += root->val;
18+
if (!root->left && !root->right && s == targetSum) return true;
19+
return dfs(root->left, s) || dfs(root->right, s);
20+
};
21+
return dfs(root, 0);
1222
}
1323
};

0 commit comments

Comments
 (0)