Skip to content

Commit b0b7036

Browse files
committed
feat: add solutions to leetcode and lcof problems
1 parent 5d0371e commit b0b7036

File tree

13 files changed

+225
-86
lines changed

13 files changed

+225
-86
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
1. [二叉树的最大深度](/solution/0100-0199/0104.Maximum%20Depth%20of%20Binary%20Tree/README.md)
9494
1. [二叉树的最小深度](/solution/0100-0199/0111.Minimum%20Depth%20of%20Binary%20Tree/README.md)
9595
1. [路径总和](/solution/0100-0199/0112.Path%20Sum/README.md)
96+
1. [路径总和 II](/solution/0100-0199/0113.Path%20Sum%20II/README.md)
9697
1. [从前序与中序遍历序列构造二叉树](/solution/0100-0199/0105.Construct%20Binary%20Tree%20from%20Preorder%20and%20Inorder%20Traversal/README.md)
9798
1. [从中序与后序遍历序列构造二叉树](/solution/0100-0199/0106.Construct%20Binary%20Tree%20from%20Inorder%20and%20Postorder%20Traversal/README.md)
9899
1. [二叉搜索树的后序遍历序列](/lcof/面试题33.%20二叉搜索树的后序遍历序列/README.md)

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
9090
1. [Maximum Depth of Binary Tree](/solution/0100-0199/0104.Maximum%20Depth%20of%20Binary%20Tree/README_EN.md)
9191
1. [Minimum Depth of Binary Tree](/solution/0100-0199/0111.Minimum%20Depth%20of%20Binary%20Tree/README_EN.md)
9292
1. [Path Sum](/solution/0100-0199/0112.Path%20Sum/README_EN.md)
93+
1. [Path Sum II](/solution/0100-0199/0113.Path%20Sum%20II/README_EN.md)
9394
1. [Construct Binary Tree from Preorder and Inorder Traversal](/solution/0100-0199/0105.Construct%20Binary%20Tree%20from%20Preorder%20and%20Inorder%20Traversal/README_EN.md)
9495
1. [Construct Binary Tree from Inorder and Postorder Traversal](/solution/0100-0199/0106.Construct%20Binary%20Tree%20from%20Inorder%20and%20Postorder%20Traversal/README_EN.md)
9596
1. [Lowest Common Ancestor of a Binary Tree](/solution/0200-0299/0236.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree/README_EN.md)

lcof/面试题34. 二叉树中和为某一值的路径/README.md

+15-17
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,21 @@
5555

5656
class Solution:
5757
def pathSum(self, root: TreeNode, sum: int) -> List[List[int]]:
58-
res, path = [], []
59-
6058
def dfs(root, sum):
61-
if not root:
59+
if root is None:
6260
return
6361
path.append(root.val)
64-
target = sum - root.val
65-
if target == 0 and not (root.left or root.right):
66-
res.append(list(path))
67-
dfs(root.left, target)
68-
dfs(root.right, target)
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)
6966
path.pop()
70-
67+
if not root:
68+
return []
69+
res = []
70+
path = []
7171
dfs(root, sum)
7272
return res
73-
7473
```
7574

7675
### **Java**
@@ -90,26 +89,25 @@ class Solution:
9089
class Solution {
9190
private List<List<Integer>> res;
9291
private List<Integer> path;
92+
9393
public List<List<Integer>> pathSum(TreeNode root, int sum) {
94+
if (root == null) return Collections.emptyList();
9495
res = new ArrayList<>();
9596
path = new ArrayList<>();
9697
dfs(root, sum);
9798
return res;
98-
9999
}
100100

101101
private void dfs(TreeNode root, int sum) {
102102
if (root == null) {
103103
return;
104104
}
105105
path.add(root.val);
106-
int target = sum - root.val;
107-
if (target == 0 && root.left == null && root.right == null) {
108-
List<Integer> t = new ArrayList<>(path);
109-
res.add(t);
106+
if (root.val == sum && root.left == null && root.right == null) {
107+
res.add(new ArrayList<>(path));
110108
}
111-
dfs(root.left, target);
112-
dfs(root.right, target);
109+
dfs(root.left, sum - root.val);
110+
dfs(root.right, sum - root.val);
113111
path.remove(path.size() - 1);
114112
}
115113
}

lcof/面试题34. 二叉树中和为某一值的路径/Solution.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,25 @@
1010
class Solution {
1111
private List<List<Integer>> res;
1212
private List<Integer> path;
13+
1314
public List<List<Integer>> pathSum(TreeNode root, int sum) {
15+
if (root == null) return Collections.emptyList();
1416
res = new ArrayList<>();
1517
path = new ArrayList<>();
1618
dfs(root, sum);
1719
return res;
18-
1920
}
2021

2122
private void dfs(TreeNode root, int sum) {
2223
if (root == null) {
2324
return;
2425
}
2526
path.add(root.val);
26-
int target = sum - root.val;
27-
if (target == 0 && root.left == null && root.right == null) {
28-
List<Integer> t = new ArrayList<>(path);
29-
res.add(t);
27+
if (root.val == sum && root.left == null && root.right == null) {
28+
res.add(new ArrayList<>(path));
3029
}
31-
dfs(root.left, target);
32-
dfs(root.right, target);
30+
dfs(root.left, sum - root.val);
31+
dfs(root.right, sum - root.val);
3332
path.remove(path.size() - 1);
3433
}
3534
}

lcof/面试题34. 二叉树中和为某一值的路径/Solution.py

+10-11
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,20 @@
55
# self.left = None
66
# self.right = None
77

8-
98
class Solution:
109
def pathSum(self, root: TreeNode, sum: int) -> List[List[int]]:
11-
res, path = [], []
12-
1310
def dfs(root, sum):
14-
if not root:
11+
if root is None:
1512
return
1613
path.append(root.val)
17-
target = sum - root.val
18-
if target == 0 and not (root.left or root.right):
19-
res.append(list(path))
20-
dfs(root.left, target)
21-
dfs(root.right, target)
14+
if root.val == sum and root.left is None and root.right is None:
15+
res.append(path.copy())
16+
dfs(root.left, sum - root.val)
17+
dfs(root.right, sum - root.val)
2218
path.pop()
23-
19+
if not root:
20+
return []
21+
res = []
22+
path = []
2423
dfs(root, sum)
25-
return res
24+
return res

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

+13-7
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@
4545

4646
class Solution:
4747
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)
48+
def dfs(root, sum):
49+
if root is None:
50+
return False
51+
if root.val == sum and root.left is None and root.right is None:
52+
return True
53+
return dfs(root.left, sum - root.val) or dfs(root.right, sum - root.val)
54+
return dfs(root, sum)
5355
```
5456

5557
### **Java**
@@ -68,9 +70,13 @@ class Solution:
6870
*/
6971
class Solution {
7072
public boolean hasPathSum(TreeNode root, int sum) {
73+
return dfs(root, sum);
74+
}
75+
76+
private boolean dfs(TreeNode root, int sum) {
7177
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);
78+
if (root.val == sum && root.left == null && root.right == null) return true;
79+
return dfs(root.left, sum - root.val) || dfs(root.right, sum - root.val);
7480
}
7581
}
7682
```

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

+13-7
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,13 @@
4848

4949
class Solution:
5050
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)
51+
def dfs(root, sum):
52+
if root is None:
53+
return False
54+
if root.val == sum and root.left is None and root.right is None:
55+
return True
56+
return dfs(root.left, sum - root.val) or dfs(root.right, sum - root.val)
57+
return dfs(root, sum)
5658
```
5759

5860
### **Java**
@@ -69,9 +71,13 @@ class Solution:
6971
*/
7072
class Solution {
7173
public boolean hasPathSum(TreeNode root, int sum) {
74+
return dfs(root, sum);
75+
}
76+
77+
private boolean dfs(TreeNode root, int sum) {
7278
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);
79+
if (root.val == sum && root.left == null && root.right == null) return true;
80+
return dfs(root.left, sum - root.val) || dfs(root.right, sum - root.val);
7581
}
7682
}
7783
```

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@
99
*/
1010
class Solution {
1111
public boolean hasPathSum(TreeNode root, int sum) {
12+
return dfs(root, sum);
13+
}
14+
15+
private boolean dfs(TreeNode root, int sum) {
1216
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);
17+
if (root.val == sum && root.left == null && root.right == null) return true;
18+
return dfs(root.left, sum - root.val) || dfs(root.right, sum - root.val);
1519
}
1620
}

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77

88
class Solution:
99
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)
10+
def dfs(root, sum):
11+
if root is None:
12+
return False
13+
if root.val == sum and root.left is None and root.right is None:
14+
return True
15+
return dfs(root.left, sum - root.val) or dfs(root.right, sum - root.val)
16+
return dfs(root, sum)

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

+58-2
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,78 @@
3333

3434
<!-- 这里可写通用的实现逻辑 -->
3535

36+
深度优先搜索+路径记录。
37+
3638
<!-- tabs:start -->
3739

3840
### **Python3**
3941

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

4244
```python
43-
45+
# Definition for a binary tree node.
46+
# class TreeNode:
47+
# def __init__(self, x):
48+
# self.val = x
49+
# self.left = None
50+
# self.right = None
51+
52+
class Solution:
53+
def pathSum(self, root: TreeNode, sum: int) -> List[List[int]]:
54+
def dfs(root, sum):
55+
if root is None:
56+
return
57+
path.append(root.val)
58+
if root.val == sum and root.left is None and root.right is None:
59+
res.append(path.copy())
60+
dfs(root.left, sum - root.val)
61+
dfs(root.right, sum - root.val)
62+
path.pop()
63+
if not root:
64+
return []
65+
res = []
66+
path = []
67+
dfs(root, sum)
68+
return res
4469
```
4570

4671
### **Java**
4772

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

5075
```java
51-
76+
/**
77+
* Definition for a binary tree node.
78+
* public class TreeNode {
79+
* int val;
80+
* TreeNode left;
81+
* TreeNode right;
82+
* TreeNode(int x) { val = x; }
83+
* }
84+
*/
85+
class Solution {
86+
private List<List<Integer>> res;
87+
private List<Integer> path;
88+
89+
public List<List<Integer>> pathSum(TreeNode root, int sum) {
90+
if (root == null) return Collections.emptyList();
91+
res = new ArrayList<>();
92+
path = new ArrayList<>();
93+
dfs(root, sum);
94+
return res;
95+
}
96+
97+
private void dfs(TreeNode root, int sum) {
98+
if (root == null) return;
99+
path.add(root.val);
100+
if (root.val == sum && root.left == null && root.right == null) {
101+
res.add(new ArrayList<>(path));
102+
}
103+
dfs(root.left, sum - root.val);
104+
dfs(root.right, sum - root.val);
105+
path.remove(path.size() - 1);
106+
}
107+
}
52108
```
53109

54110
### **...**

0 commit comments

Comments
 (0)