Skip to content

Commit 45f3351

Browse files
author
lihangqi
committed
feat: add python and java solutions to lcci problem: No.04.12
1 parent 70e8e5b commit 45f3351

File tree

5 files changed

+157
-7
lines changed

5 files changed

+157
-7
lines changed

lcci/04.10.Check SubTree/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
## 解法
3131
<!-- 这里可写通用的实现逻辑 -->
32-
先找t1中t2结点,找到后进行DFS,确认子树和t2的子树完全相同,否则返回FALSE
32+
先找 t1 中 t2 结点,找到后进行 DFS,确认子树和 t2 的子树完全相同,否则返回 FALSE
3333

3434
### Python3
3535
<!-- 这里可写当前语言的特殊实现逻辑 -->

lcci/04.12.Paths with Sum/README.md

+56-3
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,73 @@
3232

3333
## 解法
3434
<!-- 这里可写通用的实现逻辑 -->
35-
35+
DFS深度优先搜索
3636

3737
### Python3
3838
<!-- 这里可写当前语言的特殊实现逻辑 -->
39+
采用递归的思想,每递归到某个节点时:
40+
- 若root.val-sum == 0,结果加1
41+
- 考虑将此节点纳入或不纳入路径两种情况
3942

40-
```python
43+
特殊情况:若此节点的父节点在路径中,此节点必纳入路径(路径不能断)
4144

45+
```python
46+
class Solution:
47+
def pathSum(self, root: TreeNode, sum: int) -> int:
48+
def dfs(root, sum, flag):
49+
nonlocal ans
50+
if not root:
51+
return 0
52+
if sum-root.val == 0:
53+
ans += 1
54+
if flag == 0:
55+
dfs(root.left, sum, 0)
56+
dfs(root.right, sum, 0)
57+
dfs(root.left, sum-root.val, 1)
58+
dfs(root.right, sum-root.val, 1)
59+
60+
if not root:
61+
return 0
62+
ans = 0
63+
dfs(root, sum, 0)
64+
return ans
4265
```
4366

4467
### Java
4568
<!-- 这里可写当前语言的特殊实现逻辑 -->
69+
使用到2个递归过程:
4670

47-
```java
71+
- BFS:(traverse)遍历每个树节点;
72+
- DFS: 从每个树节点出发,节点求和,看是否能满足sum。
73+
74+
需要注意,节点值有正有负,需要穷尽所有的可能路径。
4875

76+
```java
77+
class Solution {
78+
int ans = 0;
79+
public int pathSum(TreeNode root, int sum) {
80+
traverse(root, sum);
81+
return ans;
82+
}
83+
84+
void traverse(TreeNode root, int sum) {
85+
if (root == null) return;
86+
ans += dfs(root, sum, 0);
87+
traverse(root.left, sum);
88+
traverse(root.right, sum);
89+
}
90+
91+
// check if sum of path is sum.
92+
int dfs(TreeNode root, int sum, int cur) {
93+
if (root == null) return 0;
94+
cur += root.val;
95+
int res = 0;
96+
if (cur == sum) res++;
97+
res += dfs(root.left, sum, cur);
98+
res += dfs(root.right, sum, cur);
99+
return res;
100+
}
101+
}
49102
```
50103

51104
### ...

lcci/04.12.Paths with Sum/README_EN.md

+56-3
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,71 @@ Given the following tree and &nbsp;<code>sum = 22,</code></p>
5757

5858

5959
## Solutions
60-
60+
Depth-First-Search
6161

6262
### Python3
63+
Using the idea of recursion, at each recursion to a node.
64+
- If root.val-sum == 0, add 1 to the result
65+
- Consider two scenarios for inclusion or exclusion of this node from the pathway
6366

64-
```python
67+
Special case: if the parent node of this node is in the path, this node must be included in the path (the path cannot be broken)
6568

69+
```python
70+
class Solution:
71+
def pathSum(self, root: TreeNode, sum: int) -> int:
72+
def dfs(root, sum, flag):
73+
nonlocal ans
74+
if not root:
75+
return 0
76+
if sum-root.val == 0:
77+
ans += 1
78+
if flag == 0:
79+
dfs(root.left, sum, 0)
80+
dfs(root.right, sum, 0)
81+
dfs(root.left, sum-root.val, 1)
82+
dfs(root.right, sum-root.val, 1)
83+
84+
if not root:
85+
return 0
86+
ans = 0
87+
dfs(root, sum, 0)
88+
return ans
6689
```
6790

6891
### Java
92+
Use to 2 recursive processes.
6993

70-
```java
94+
- BFS: (traverse) traverses each tree node.
95+
- DFS: Starting from each tree node, the nodes sum to see if sum can be satisfied.
96+
97+
Note that node values can be positive or negative, and all possible paths need to be exhausted.
7198

99+
```java
100+
class Solution {
101+
int ans = 0;
102+
public int pathSum(TreeNode root, int sum) {
103+
traverse(root, sum);
104+
return ans;
105+
}
106+
107+
void traverse(TreeNode root, int sum) {
108+
if (root == null) return;
109+
ans += dfs(root, sum, 0);
110+
traverse(root.left, sum);
111+
traverse(root.right, sum);
112+
}
113+
114+
// check if sum of path is sum.
115+
int dfs(TreeNode root, int sum, int cur) {
116+
if (root == null) return 0;
117+
cur += root.val;
118+
int res = 0;
119+
if (cur == sum) res++;
120+
res += dfs(root.left, sum, cur);
121+
res += dfs(root.right, sum, cur);
122+
return res;
123+
}
124+
}
72125
```
73126

74127
### ...
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
int ans = 0;
3+
public int pathSum(TreeNode root, int sum) {
4+
traverse(root, sum);
5+
return ans;
6+
}
7+
8+
void traverse(TreeNode root, int sum) {
9+
if (root == null) return;
10+
ans += dfs(root, sum, 0);
11+
traverse(root.left, sum);
12+
traverse(root.right, sum);
13+
}
14+
15+
// check if sum of path is sum.
16+
int dfs(TreeNode root, int sum, int cur) {
17+
if (root == null) return 0;
18+
cur += root.val;
19+
int res = 0;
20+
if (cur == sum) res++;
21+
res += dfs(root.left, sum, cur);
22+
res += dfs(root.right, sum, cur);
23+
return res;
24+
}
25+
}

lcci/04.12.Paths with Sum/Solution.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def pathSum(self, root: TreeNode, sum: int) -> int:
3+
def dfs(root, sum, flag):
4+
nonlocal ans
5+
if not root:
6+
return 0
7+
if sum-root.val == 0:
8+
ans += 1
9+
if flag == 0:
10+
dfs(root.left, sum, 0)
11+
dfs(root.right, sum, 0)
12+
dfs(root.left, sum-root.val, 1)
13+
dfs(root.right, sum-root.val, 1)
14+
15+
if not root:
16+
return 0
17+
ans = 0
18+
dfs(root, sum, 0)
19+
return ans

0 commit comments

Comments
 (0)