Skip to content

Commit 1ef234b

Browse files
committed
feat: add solutions to leetcode problem: No.0112
See https://leetcode-cn.com/problems/path-sum/
1 parent 7606f0d commit 1ef234b

File tree

6 files changed

+82
-21
lines changed

6 files changed

+82
-21
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
1. [二叉树的层次遍历 II](/solution/0100-0199/0107.Binary%20Tree%20Level%20Order%20Traversal%20II/README.md)
8989
1. [二叉树的最大深度](/solution/0100-0199/0104.Maximum%20Depth%20of%20Binary%20Tree/README.md)
9090
1. [二叉树的最小深度](/solution/0100-0199/0111.Minimum%20Depth%20of%20Binary%20Tree/README.md)
91+
1. [路径总和](/solution/0100-0199/0112.Path%20Sum/README.md)
9192
1. [从前序与中序遍历序列构造二叉树](/solution/0100-0199/0105.Construct%20Binary%20Tree%20from%20Preorder%20and%20Inorder%20Traversal/README.md)
9293
1. [从中序与后序遍历序列构造二叉树](/solution/0100-0199/0106.Construct%20Binary%20Tree%20from%20Inorder%20and%20Postorder%20Traversal/README.md)
9394
1. [二叉树的最近公共祖先](/solution/0200-0299/0235.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree/README.md)

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
8686
1. [Binary Tree Level Order Traversal II](/solution/0100-0199/0107.Binary%20Tree%20Level%20Order%20Traversal%20II/README_EN.md)
8787
1. [Maximum Depth of Binary Tree](/solution/0100-0199/0104.Maximum%20Depth%20of%20Binary%20Tree/README_EN.md)
8888
1. [Minimum Depth of Binary Tree](/solution/0100-0199/0111.Minimum%20Depth%20of%20Binary%20Tree/README_EN.md)
89+
1. [Path Sum](/solution/0100-0199/0112.Path%20Sum/README_EN.md)
8990
1. [Construct Binary Tree from Preorder and Inorder Traversal](/solution/0100-0199/0105.Construct%20Binary%20Tree%20from%20Preorder%20and%20Inorder%20Traversal/README_EN.md)
9091
1. [Construct Binary Tree from Inorder and Postorder Traversal](/solution/0100-0199/0106.Construct%20Binary%20Tree%20from%20Inorder%20and%20Postorder%20Traversal/README_EN.md)
9192
1. [Lowest Common Ancestor of a Binary Tree](/solution/0200-0299/0236.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree/README_EN.md)

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

+32-2
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,52 @@
2727

2828
<!-- 这里可写通用的实现逻辑 -->
2929

30+
递归求解,递归地询问它的子节点是否能满足条件即可。
31+
3032
<!-- tabs:start -->
3133

3234
### **Python3**
3335

3436
<!-- 这里可写当前语言的特殊实现逻辑 -->
3537

3638
```python
37-
39+
# Definition for a binary tree node.
40+
# class TreeNode:
41+
# def __init__(self, x):
42+
# self.val = x
43+
# self.left = None
44+
# self.right = None
45+
46+
class Solution:
47+
def hasPathSum(self, root: TreeNode, sum: int) -> bool:
48+
if root is None:
49+
return False
50+
if root.left is None and root.right is None:
51+
return root.val == sum
52+
return self.hasPathSum(root.left, sum - root.val) or self.hasPathSum(root.right, sum - root.val)
3853
```
3954

4055
### **Java**
4156

4257
<!-- 这里可写当前语言的特殊实现逻辑 -->
4358

4459
```java
45-
60+
/**
61+
* Definition for a binary tree node.
62+
* public class TreeNode {
63+
* int val;
64+
* TreeNode left;
65+
* TreeNode right;
66+
* TreeNode(int x) { val = x; }
67+
* }
68+
*/
69+
class Solution {
70+
public boolean hasPathSum(TreeNode root, int sum) {
71+
if (root == null) return false;
72+
if (root.left == null && root.right == null) return sum == root.val;
73+
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
74+
}
75+
}
4676
```
4777

4878
### **...**

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

+30-2
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,41 @@
3939
### **Python3**
4040

4141
```python
42-
42+
# Definition for a binary tree node.
43+
# class TreeNode:
44+
# def __init__(self, x):
45+
# self.val = x
46+
# self.left = None
47+
# self.right = None
48+
49+
class Solution:
50+
def hasPathSum(self, root: TreeNode, sum: int) -> bool:
51+
if root is None:
52+
return False
53+
if root.left is None and root.right is None:
54+
return root.val == sum
55+
return self.hasPathSum(root.left, sum - root.val) or self.hasPathSum(root.right, sum - root.val)
4356
```
4457

4558
### **Java**
4659

4760
```java
48-
61+
/**
62+
* Definition for a binary tree node.
63+
* public class TreeNode {
64+
* int val;
65+
* TreeNode left;
66+
* TreeNode right;
67+
* TreeNode(int x) { val = x; }
68+
* }
69+
*/
70+
class Solution {
71+
public boolean hasPathSum(TreeNode root, int sum) {
72+
if (root == null) return false;
73+
if (root.left == null && root.right == null) return sum == root.val;
74+
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
75+
}
76+
}
4977
```
5078

5179
### **...**

solution/0100-0199/0112.Path Sum/Solution.java

+4-17
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,9 @@
88
* }
99
*/
1010
class Solution {
11-
public static boolean hasPathSum(TreeNode root, int sum) {
12-
if (root == null) {
13-
return false;
14-
}
15-
return hasPathSum(root, 0, sum);
16-
}
17-
18-
public static boolean hasPathSum(TreeNode root, int sum, int target) {
19-
if (root == null) {
20-
return false;
21-
}
22-
if (root.left == null && root.right == null) {
23-
return sum + root.val == target;
24-
}
25-
26-
sum += root.val;
27-
return hasPathSum(root.left, sum, target) || hasPathSum(root.right, sum, target);
11+
public boolean hasPathSum(TreeNode root, int sum) {
12+
if (root == null) return false;
13+
if (root.left == null && root.right == null) return sum == root.val;
14+
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
2815
}
2916
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution:
9+
def hasPathSum(self, root: TreeNode, sum: int) -> bool:
10+
if root is None:
11+
return False
12+
if root.left is None and root.right is None:
13+
return root.val == sum
14+
return self.hasPathSum(root.left, sum - root.val) or self.hasPathSum(root.right, sum - root.val)

0 commit comments

Comments
 (0)