Skip to content

Commit 88aaf42

Browse files
authored
Merge pull request doocs#278 from lihangqi/master
feat: add python and java solutions to lcci problem: No.04.10 and No.04.12
2 parents b4c3193 + 25350ec commit 88aaf42

File tree

8 files changed

+266
-12
lines changed

8 files changed

+266
-12
lines changed

lcci/04.10.Check SubTree/README.md

+37-3
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,54 @@
2929

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

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

3737
```python
38-
38+
class Solution:
39+
def checkSubTree(self, t1: TreeNode, t2: TreeNode) -> bool:
40+
if t1 == None:
41+
return False
42+
if t2 == None:
43+
return True
44+
return self.dfs(t1,t2) or self.checkSubTree(t1.left,t2) or self.checkSubTree(t1.right,t2)
45+
46+
def dfs(self, t1: TreeNode, t2: TreeNode) -> bool:
47+
if not t1 and t2 :
48+
return False
49+
if not t2 and not t1:
50+
return True
51+
if t1.val != t2.val:
52+
return False
53+
else:
54+
return self.dfs(t1.left,t2.left) and self.dfs(t1.right,t2.right)
3955
```
4056

4157
### Java
4258
<!-- 这里可写当前语言的特殊实现逻辑 -->
4359

4460
```java
45-
61+
class Solution {
62+
public boolean checkSubTree(TreeNode t1, TreeNode t2) {
63+
if (t2 == null)
64+
return true;
65+
if (t1 == null)
66+
return false;
67+
return isSubTree(t1, t2) || checkSubTree(t1.left, t2) || checkSubTree(t1.right, t2);
68+
}
69+
70+
public boolean isSubTree(TreeNode t1, TreeNode t2){
71+
if (t2 == null)
72+
return true;
73+
if (t1 == null)
74+
return false;
75+
if (t1.val != t2.val)
76+
return false;
77+
return isSubTree(t1.left,t2.left) && isSubTree(t1.right,t2.right);
78+
}
79+
}
4680
```
4781

4882
### ...

lcci/04.10.Check SubTree/README_EN.md

+37-3
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,52 @@
5151

5252

5353
## Solutions
54-
54+
Find the t2 node in t1 first, then use the depth-first search (DFS) algorithm to make sure that the subtree and the subtree of t2 are identical, otherwise return FALSE.
5555

5656
### Python3
5757

5858
```python
59-
59+
class Solution:
60+
def checkSubTree(self, t1: TreeNode, t2: TreeNode) -> bool:
61+
if t1 == None:
62+
return False
63+
if t2 == None:
64+
return True
65+
return self.dfs(t1,t2) or self.checkSubTree(t1.left,t2) or self.checkSubTree(t1.right,t2)
66+
67+
def dfs(self, t1: TreeNode, t2: TreeNode) -> bool:
68+
if not t1 and t2 :
69+
return False
70+
if not t2 and not t1:
71+
return True
72+
if t1.val != t2.val:
73+
return False
74+
else:
75+
return self.dfs(t1.left,t2.left) and self.dfs(t1.right,t2.right)
6076
```
6177

6278
### Java
6379

6480
```java
65-
81+
class Solution {
82+
public boolean checkSubTree(TreeNode t1, TreeNode t2) {
83+
if (t2 == null)
84+
return true;
85+
if (t1 == null)
86+
return false;
87+
return isSubTree(t1, t2) || checkSubTree(t1.left, t2) || checkSubTree(t1.right, t2);
88+
}
89+
90+
public boolean isSubTree(TreeNode t1, TreeNode t2){
91+
if (t2 == null)
92+
return true;
93+
if (t1 == null)
94+
return false;
95+
if (t1.val != t2.val)
96+
return false;
97+
return isSubTree(t1.left,t2.left) && isSubTree(t1.right,t2.right);
98+
}
99+
}
66100
```
67101

68102
### ...
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public boolean checkSubTree(TreeNode t1, TreeNode t2) {
3+
if (t2 == null)
4+
return true;
5+
if (t1 == null)
6+
return false;
7+
return isSubTree(t1, t2) || checkSubTree(t1.left, t2) || checkSubTree(t1.right, t2);
8+
}
9+
10+
public boolean isSubTree(TreeNode t1, TreeNode t2){
11+
if (t2 == null)
12+
return true;
13+
if (t1 == null)
14+
return false;
15+
if (t1.val != t2.val)
16+
return false;
17+
return isSubTree(t1.left,t2.left) && isSubTree(t1.right,t2.right);
18+
}
19+
}

lcci/04.10.Check SubTree/Solution.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def checkSubTree(self, t1: TreeNode, t2: TreeNode) -> bool:
3+
if t1 == None:
4+
return False
5+
if t2 == None:
6+
return True
7+
return self.dfs(t1,t2) or self.checkSubTree(t1.left,t2) or self.checkSubTree(t1.right,t2)
8+
9+
def dfs(self, t1: TreeNode, t2: TreeNode) -> bool:
10+
if not t1 and t2 :
11+
return False
12+
if not t2 and not t1:
13+
return True
14+
if t1.val != t2.val:
15+
return False
16+
else:
17+
return self.dfs(t1.left,t2.left) and self.dfs(t1.right,t2.right)

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)