Skip to content

Commit a1f6eb5

Browse files
author
lihangqi
committed
feat: add python and java solutions to lcci problem: No.04.10
1 parent b4c3193 commit a1f6eb5

File tree

4 files changed

+110
-6
lines changed

4 files changed

+110
-6
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,t2):
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,t2):
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,t2):
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,t2):
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,t2):
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,t2):
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)

0 commit comments

Comments
 (0)