Skip to content

Commit 1b12dcc

Browse files
committedJan 26, 2022
feat: add solutions to lcci problem: No.04.10
No.04.10.Check SubTree
1 parent 4061c1c commit 1b12dcc

File tree

10 files changed

+282
-132
lines changed

10 files changed

+282
-132
lines changed
 

‎basic/sorting/InsertionSort/InsertionSort.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@ public class Program
55
{
66
public static void Main()
77
{
8-
int[] test = new int[]{31, 12, 10, 5, 6, 7, 8, 10, 23, 34, 56, 43, 32, 21};
8+
int[] test = new int[] { 31, 12, 10, 5, 6, 7, 8, 10, 23, 34, 56, 43, 32, 21 };
99
InsertSortNums(test);
1010
foreach (var item in test)
1111
{
12-
WriteLine(item);
12+
WriteLine(item);
1313
}
1414
}
1515
public static void InsertSortNums(int[] nums)
1616
{
17-
for(int initial = 1; initial < nums.Length; initial++)
18-
{
19-
for(int second_sort = 0; second_sort < initial; second_sort++)
20-
{
21-
if(nums[second_sort] > nums[initial])
17+
for (int initial = 1; initial < nums.Length; initial++)
18+
{
19+
for (int second_sort = 0; second_sort < initial; second_sort++)
20+
{
21+
if (nums[second_sort] > nums[initial])
2222
{
23-
swap(ref nums[second_sort], ref nums[initial]);
23+
swap(ref nums[second_sort], ref nums[initial]);
2424
}
2525
}
2626
}

‎basic/sorting/InsertionSort/README.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -167,22 +167,22 @@ public class Program
167167
{
168168
public static void Main()
169169
{
170-
int[] test = new int[]{31, 12, 10, 5, 6, 7, 8, 10, 23, 34, 56, 43, 32, 21};
170+
int[] test = new int[] { 31, 12, 10, 5, 6, 7, 8, 10, 23, 34, 56, 43, 32, 21 };
171171
InsertSortNums(test);
172172
foreach (var item in test)
173173
{
174-
WriteLine(item);
174+
WriteLine(item);
175175
}
176176
}
177177
public static void InsertSortNums(int[] nums)
178178
{
179-
for(int initial = 1; initial < nums.Length; initial++)
180-
{
181-
for(int second_sort = 0; second_sort < initial; second_sort++)
182-
{
183-
if(nums[second_sort] > nums[initial])
179+
for (int initial = 1; initial < nums.Length; initial++)
180+
{
181+
for (int second_sort = 0; second_sort < initial; second_sort++)
182+
{
183+
if (nums[second_sort] > nums[initial])
184184
{
185-
swap(ref nums[second_sort], ref nums[initial]);
185+
swap(ref nums[second_sort], ref nums[initial]);
186186
}
187187
}
188188
}

‎lcci/04.10.Check SubTree/README.md

+80-28
Original file line numberDiff line numberDiff line change
@@ -31,57 +31,109 @@
3131

3232
<!-- 这里可写通用的实现逻辑 -->
3333

34-
先找 t1 中 t2 结点,找到后进行 DFS,确认子树和 t2 的子树完全相同,否则返回 FALSE。
35-
3634
<!-- tabs:start -->
3735

3836
### **Python3**
3937

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

4240
```python
41+
# Definition for a binary tree node.
42+
# class TreeNode:
43+
# def __init__(self, x):
44+
# self.val = x
45+
# self.left = None
46+
# self.right = None
47+
4348
class Solution:
4449
def checkSubTree(self, t1: TreeNode, t2: TreeNode) -> bool:
45-
if t1 == None:
46-
return False
47-
if t2 == None:
48-
return True
49-
return self.dfs(t1,t2) or self.checkSubTree(t1.left,t2) or self.checkSubTree(t1.right,t2)
50-
51-
def dfs(self, t1: TreeNode, t2: TreeNode) -> bool:
52-
if not t1 and t2 :
53-
return False
54-
if not t2 and not t1:
55-
return True
56-
if t1.val != t2.val:
57-
return False
58-
else:
59-
return self.dfs(t1.left,t2.left) and self.dfs(t1.right,t2.right)
50+
def dfs(t1, t2):
51+
if t2 is None:
52+
return True
53+
if t1 is None:
54+
return False
55+
if t1.val == t2.val:
56+
return dfs(t1.left, t2.left) and dfs(t1.right, t2.right)
57+
return dfs(t1.left, t2) or dfs(t1.right, t2)
58+
59+
return dfs(t1, t2)
6060
```
6161

6262
### **Java**
6363

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

6666
```java
67+
/**
68+
* Definition for a binary tree node.
69+
* public class TreeNode {
70+
* int val;
71+
* TreeNode left;
72+
* TreeNode right;
73+
* TreeNode(int x) { val = x; }
74+
* }
75+
*/
6776
class Solution {
6877
public boolean checkSubTree(TreeNode t1, TreeNode t2) {
69-
if (t2 == null)
78+
if (t2 == null) {
7079
return true;
71-
if (t1 == null)
80+
}
81+
if (t1 == null) {
7282
return false;
73-
return isSubTree(t1, t2) || checkSubTree(t1.left, t2) || checkSubTree(t1.right, t2);
83+
}
84+
if (t1.val == t2.val) {
85+
return checkSubTree(t1.left, t2.left) && checkSubTree(t1.right, t2.right);
86+
}
87+
return checkSubTree(t1.left, t2) || checkSubTree(t1.right, t2);
7488
}
89+
}
90+
```
7591

76-
public boolean isSubTree(TreeNode t1, TreeNode t2){
77-
if (t2 == null)
78-
return true;
79-
if (t1 == null)
80-
return false;
81-
if (t1.val != t2.val)
82-
return false;
83-
return isSubTree(t1.left,t2.left) && isSubTree(t1.right,t2.right);
92+
### **C++**
93+
94+
```cpp
95+
/**
96+
* Definition for a binary tree node.
97+
* struct TreeNode {
98+
* int val;
99+
* TreeNode *left;
100+
* TreeNode *right;
101+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
102+
* };
103+
*/
104+
class Solution {
105+
public:
106+
bool checkSubTree(TreeNode* t1, TreeNode* t2) {
107+
if (!t2) return 1;
108+
if (!t1) return 0;
109+
if (t1->val == t2->val) return checkSubTree(t1->left, t2->left) && checkSubTree(t1->right, t2->right);
110+
return checkSubTree(t1->left, t2) || checkSubTree(t1->right, t2);
84111
}
112+
};
113+
```
114+
115+
### **Go**
116+
117+
```go
118+
/**
119+
* Definition for a binary tree node.
120+
* type TreeNode struct {
121+
* Val int
122+
* Left *TreeNode
123+
* Right *TreeNode
124+
* }
125+
*/
126+
func checkSubTree(t1 *TreeNode, t2 *TreeNode) bool {
127+
if t2 == nil {
128+
return true
129+
}
130+
if t1 == nil {
131+
return false
132+
}
133+
if t1.Val == t2.Val {
134+
return checkSubTree(t1.Left, t2.Left) && checkSubTree(t1.Right, t2.Right)
135+
}
136+
return checkSubTree(t1.Left, t2) || checkSubTree(t1.Right, t2)
85137
}
86138
```
87139

‎lcci/04.10.Check SubTree/README_EN.md

+80-26
Original file line numberDiff line numberDiff line change
@@ -43,46 +43,100 @@ Find the t2 node in t1 first, then use the depth-first search (DFS) algorithm to
4343
### **Python3**
4444

4545
```python
46+
# Definition for a binary tree node.
47+
# class TreeNode:
48+
# def __init__(self, x):
49+
# self.val = x
50+
# self.left = None
51+
# self.right = None
52+
4653
class Solution:
4754
def checkSubTree(self, t1: TreeNode, t2: TreeNode) -> bool:
48-
if t1 == None:
49-
return False
50-
if t2 == None:
51-
return True
52-
return self.dfs(t1,t2) or self.checkSubTree(t1.left,t2) or self.checkSubTree(t1.right,t2)
53-
54-
def dfs(self, t1: TreeNode, t2: TreeNode) -> bool:
55-
if not t1 and t2 :
56-
return False
57-
if not t2 and not t1:
58-
return True
59-
if t1.val != t2.val:
60-
return False
61-
else:
62-
return self.dfs(t1.left,t2.left) and self.dfs(t1.right,t2.right)
55+
def dfs(t1, t2):
56+
if t2 is None:
57+
return True
58+
if t1 is None:
59+
return False
60+
if t1.val == t2.val:
61+
return dfs(t1.left, t2.left) and dfs(t1.right, t2.right)
62+
return dfs(t1.left, t2) or dfs(t1.right, t2)
63+
64+
return dfs(t1, t2)
6365
```
6466

6567
### **Java**
6668

6769
```java
70+
/**
71+
* Definition for a binary tree node.
72+
* public class TreeNode {
73+
* int val;
74+
* TreeNode left;
75+
* TreeNode right;
76+
* TreeNode(int x) { val = x; }
77+
* }
78+
*/
6879
class Solution {
6980
public boolean checkSubTree(TreeNode t1, TreeNode t2) {
70-
if (t2 == null)
81+
if (t2 == null) {
7182
return true;
72-
if (t1 == null)
83+
}
84+
if (t1 == null) {
7385
return false;
74-
return isSubTree(t1, t2) || checkSubTree(t1.left, t2) || checkSubTree(t1.right, t2);
86+
}
87+
if (t1.val == t2.val) {
88+
return checkSubTree(t1.left, t2.left) && checkSubTree(t1.right, t2.right);
89+
}
90+
return checkSubTree(t1.left, t2) || checkSubTree(t1.right, t2);
7591
}
92+
}
93+
```
7694

77-
public boolean isSubTree(TreeNode t1, TreeNode t2){
78-
if (t2 == null)
79-
return true;
80-
if (t1 == null)
81-
return false;
82-
if (t1.val != t2.val)
83-
return false;
84-
return isSubTree(t1.left,t2.left) && isSubTree(t1.right,t2.right);
95+
### **C++**
96+
97+
```cpp
98+
/**
99+
* Definition for a binary tree node.
100+
* struct TreeNode {
101+
* int val;
102+
* TreeNode *left;
103+
* TreeNode *right;
104+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
105+
* };
106+
*/
107+
class Solution {
108+
public:
109+
bool checkSubTree(TreeNode* t1, TreeNode* t2) {
110+
if (!t2) return 1;
111+
if (!t1) return 0;
112+
if (t1->val == t2->val) return checkSubTree(t1->left, t2->left) && checkSubTree(t1->right, t2->right);
113+
return checkSubTree(t1->left, t2) || checkSubTree(t1->right, t2);
85114
}
115+
};
116+
```
117+
118+
### **Go**
119+
120+
```go
121+
/**
122+
* Definition for a binary tree node.
123+
* type TreeNode struct {
124+
* Val int
125+
* Left *TreeNode
126+
* Right *TreeNode
127+
* }
128+
*/
129+
func checkSubTree(t1 *TreeNode, t2 *TreeNode) bool {
130+
if t2 == nil {
131+
return true
132+
}
133+
if t1 == nil {
134+
return false
135+
}
136+
if t1.Val == t2.Val {
137+
return checkSubTree(t1.Left, t2.Left) && checkSubTree(t1.Right, t2.Right)
138+
}
139+
return checkSubTree(t1.Left, t2) || checkSubTree(t1.Right, t2)
86140
}
87141
```
88142

‎lcci/04.10.Check SubTree/Solution.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
public:
12+
bool checkSubTree(TreeNode* t1, TreeNode* t2) {
13+
if (!t2) return 1;
14+
if (!t1) return 0;
15+
if (t1->val == t2->val) return checkSubTree(t1->left, t2->left) && checkSubTree(t1->right, t2->right);
16+
return checkSubTree(t1->left, t2) || checkSubTree(t1->right, t2);
17+
}
18+
};

‎lcci/04.10.Check SubTree/Solution.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
func checkSubTree(t1 *TreeNode, t2 *TreeNode) bool {
10+
if t2 == nil {
11+
return true
12+
}
13+
if t1 == nil {
14+
return false
15+
}
16+
if t1.Val == t2.Val {
17+
return checkSubTree(t1.Left, t2.Left) && checkSubTree(t1.Right, t2.Right)
18+
}
19+
return checkSubTree(t1.Left, t2) || checkSubTree(t1.Right, t2)
20+
}

0 commit comments

Comments
 (0)
Please sign in to comment.