Skip to content

Commit 43cf1cf

Browse files
committed
feat: add solutions to lcof question: 34.pathSum
添加《剑指 Offer》题解:面试题34. 二叉树中和为某一值的路径
1 parent 42cbf49 commit 43cf1cf

File tree

9 files changed

+180
-13
lines changed

9 files changed

+180
-13
lines changed

lcof/面试题04. 二维数组中的查找/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ class Solution:
3535
if not matrix:
3636
return False
3737

38-
i = len(matrix) - 1
39-
j = 0
38+
i, j = len(matrix) - 1, 0
4039
while i >= 0 and j < len(matrix[0]):
4140
if matrix[i][j] == target:
4241
return True

lcof/面试题04. 二维数组中的查找/Solution.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ def findNumberIn2DArray(self, matrix: List[List[int]], target: int) -> bool:
33
if not matrix:
44
return False
55

6-
i = len(matrix) - 1
7-
j = 0
6+
i, j = len(matrix) - 1, 0
87
while i >= 0 and j < len(matrix[0]):
98
if matrix[i][j] == target:
109
return True

lcof/面试题10- I. 斐波那契数列/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ class Solution:
3737
def fib(self, n: int) -> int:
3838
a, b = 0, 1
3939
for _ in range(n):
40-
s = a + b
41-
a, b = b, s
40+
a, b = b, a + b
4241
return a % 1000000007
4342
```
4443

lcof/面试题10- I. 斐波那契数列/Solution.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@ class Solution:
22
def fib(self, n: int) -> int:
33
a, b = 0, 1
44
for _ in range(n):
5-
s = a + b
6-
a, b = b, s
5+
a, b = b, a + b
76
return a % 1000000007

lcof/面试题10- II. 青蛙跳台阶问题/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ class Solution:
3131
def numWays(self, n: int) -> int:
3232
a, b = 0, 1
3333
for _ in range(n):
34-
s = a + b
35-
a, b = b, s
34+
a, b = b, a + b
3635
return b % 1000000007
3736
```
3837

lcof/面试题10- II. 青蛙跳台阶问题/Solution.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@ class Solution:
22
def numWays(self, n: int) -> int:
33
a, b = 0, 1
44
for _ in range(n):
5-
s = a + b
6-
a, b = b, s
7-
return b % 1000000007
5+
a, b = b, a + b
6+
return b % 1000000007
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# [面试题34. 二叉树中和为某一值的路径](https://leetcode-cn.com/problems/er-cha-shu-zhong-he-wei-mou-yi-zhi-de-lu-jing-lcof/)
2+
3+
## 题目描述
4+
<!-- 这里写题目描述 -->
5+
输入一棵二叉树和一个整数,打印出二叉树中节点值的和为输入整数的所有路径。从树的根节点开始往下一直到叶节点所经过的节点形成一条路径。
6+
7+
**示例:**
8+
9+
给定如下二叉树,以及目标和 `sum = 22`
10+
11+
```
12+
5
13+
/ \
14+
4 8
15+
/ / \
16+
11 13 4
17+
/ \ / \
18+
7 2 5 1
19+
```
20+
21+
返回:
22+
23+
```
24+
[
25+
[5,4,11,2],
26+
[5,8,4,5]
27+
]
28+
```
29+
30+
**提示:**
31+
32+
1. `节点总数 <= 10000`
33+
34+
## 解法
35+
<!-- 这里可写通用的实现逻辑 -->
36+
先序遍历+路径记录。
37+
38+
### Python3
39+
<!-- 这里可写当前语言的特殊实现逻辑 -->
40+
41+
```python
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 pathSum(self, root: TreeNode, sum: int) -> List[List[int]]:
51+
res, path = [], []
52+
53+
def dfs(root, sum):
54+
if not root:
55+
return
56+
path.append(root.val)
57+
target = sum - root.val
58+
if target == 0 and not (root.left or root.right):
59+
res.append(list(path))
60+
dfs(root.left, target)
61+
dfs(root.right, target)
62+
path.pop()
63+
64+
dfs(root, sum)
65+
return res
66+
67+
```
68+
69+
### Java
70+
<!-- 这里可写当前语言的特殊实现逻辑 -->
71+
72+
```java
73+
/**
74+
* Definition for a binary tree node.
75+
* public class TreeNode {
76+
* int val;
77+
* TreeNode left;
78+
* TreeNode right;
79+
* TreeNode(int x) { val = x; }
80+
* }
81+
*/
82+
class Solution {
83+
private List<List<Integer>> res;
84+
private List<Integer> path;
85+
public List<List<Integer>> pathSum(TreeNode root, int sum) {
86+
res = new ArrayList<>();
87+
path = new ArrayList<>();
88+
dfs(root, sum);
89+
return res;
90+
91+
}
92+
93+
private void dfs(TreeNode root, int sum) {
94+
if (root == null) {
95+
return;
96+
}
97+
path.add(root.val);
98+
int target = sum - root.val;
99+
if (target == 0 && root.left == null && root.right == null) {
100+
List<Integer> t = new ArrayList<>(path);
101+
res.add(t);
102+
}
103+
dfs(root.left, target);
104+
dfs(root.right, target);
105+
path.remove(path.size() - 1);
106+
}
107+
}
108+
```
109+
110+
### ...
111+
```
112+
113+
```
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
private List<List<Integer>> res;
12+
private List<Integer> path;
13+
public List<List<Integer>> pathSum(TreeNode root, int sum) {
14+
res = new ArrayList<>();
15+
path = new ArrayList<>();
16+
dfs(root, sum);
17+
return res;
18+
19+
}
20+
21+
private void dfs(TreeNode root, int sum) {
22+
if (root == null) {
23+
return;
24+
}
25+
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);
30+
}
31+
dfs(root.left, target);
32+
dfs(root.right, target);
33+
path.remove(path.size() - 1);
34+
}
35+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
9+
class Solution:
10+
def pathSum(self, root: TreeNode, sum: int) -> List[List[int]]:
11+
res, path = [], []
12+
13+
def dfs(root, sum):
14+
if not root:
15+
return
16+
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)
22+
path.pop()
23+
24+
dfs(root, sum)
25+
return res

0 commit comments

Comments
 (0)