Skip to content

Commit cdf099e

Browse files
committed
feat: add solutions to lcof problem: No.26
1 parent 8a421fe commit cdf099e

File tree

4 files changed

+96
-74
lines changed

4 files changed

+96
-74
lines changed

lcof/面试题26. 树的子结构/README.md

+72-53
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,22 @@
4040

4141
<!-- tabs:start -->
4242

43+
**方法一:DFS**
44+
45+
我们先判断 `A``B` 是否为空,如果 `A``B` 为空,直接返回 `false`
46+
47+
然后我们定义一个 `dfs(A, B)` 函数,用于判断从 `A` 的根节点开始,是否存在一棵子树和 `B` 的结构相同,如果存在,返回 `true`,否则返回 `false`
48+
49+
`dfs` 函数中,我们首先判断 `B` 是否为空,如果 `B` 为空,说明 `A` 的子树和 `B` 的结构相同,返回 `true`
50+
51+
然后我们判断 `A` 是否为空,或者 `A``B` 的根节点值是否相同,如果 `A` 为空,或者 `A``B` 的根节点值不相同,说明 `A` 的子树和 `B` 的结构不同,返回 `false`
52+
53+
最后我们返回 `dfs(A.left, B.left) and dfs(A.right, B.right)`,即 `A` 的左子树和 `B` 的左子树是否相同,以及 `A` 的右子树和 `B` 的右子树是否相同。
54+
55+
最后我们返回 `dfs(A, B) or isSubStructure(A.left, B) or isSubStructure(A.right, B)`,即 `A` 的子树和 `B` 的结构是否相同,或者 `A` 的左子树和 `B` 的结构是否相同,或者 `A` 的右子树和 `B` 的结构是否相同。
56+
57+
时间复杂度 $O(m \times n)$,空间复杂度 $O(\max(m, n))$。其中 $m$ 和 $n$ 分别为树 `A``B` 的节点数。
58+
4359
### **Python3**
4460

4561
```python
@@ -62,11 +78,9 @@ class Solution:
6278

6379
if A is None or B is None:
6480
return False
65-
return (
66-
dfs(A, B)
67-
or self.isSubStructure(A.left, B)
68-
or self.isSubStructure(A.right, B)
69-
)
81+
if dfs(A, B):
82+
return True
83+
return self.isSubStructure(A.left, B) or self.isSubStructure(A.right, B)
7084
```
7185

7286
### **Java**
@@ -101,29 +115,30 @@ class Solution {
101115
}
102116
```
103117

104-
### **JavaScript**
118+
### **C++**
105119

106-
```js
120+
```cpp
107121
/**
108122
* Definition for a binary tree node.
109-
* function TreeNode(val) {
110-
* this.val = val;
111-
* this.left = this.right = null;
112-
* }
113-
*/
114-
/**
115-
* @param {TreeNode} A
116-
* @param {TreeNode} B
117-
* @return {boolean}
123+
* struct TreeNode {
124+
* int val;
125+
* TreeNode *left;
126+
* TreeNode *right;
127+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
128+
* };
118129
*/
119-
var isSubStructure = function (A, B) {
120-
function dfs(A, B) {
121-
if (!B) return true;
122-
if (!A || A.val != B.val) return false;
123-
return dfs(A.left, B.left) && dfs(A.right, B.right);
130+
class Solution {
131+
public:
132+
bool isSubStructure(TreeNode* A, TreeNode* B) {
133+
if (!A || !B) return 0;
134+
return dfs(A, B) || isSubStructure(A->left, B) || isSubStructure(A->right, B);
135+
}
136+
137+
bool dfs(TreeNode* A, TreeNode* B) {
138+
if (!B) return 1;
139+
if (!A || A->val != B->val) return 0;
140+
return dfs(A->left, B->left) && dfs(A->right, B->right);
124141
}
125-
if (!A || !B) return false;
126-
return dfs(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B);
127142
};
128143
```
129144

@@ -156,30 +171,35 @@ func isSubStructure(A *TreeNode, B *TreeNode) bool {
156171
}
157172
```
158173

159-
### **C++**
174+
### **JavaScript**
160175

161-
```cpp
176+
```js
162177
/**
163178
* Definition for a binary tree node.
164-
* struct TreeNode {
165-
* int val;
166-
* TreeNode *left;
167-
* TreeNode *right;
168-
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
169-
* };
179+
* function TreeNode(val) {
180+
* this.val = val;
181+
* this.left = this.right = null;
182+
* }
170183
*/
171-
class Solution {
172-
public:
173-
bool isSubStructure(TreeNode* A, TreeNode* B) {
174-
if (!A || !B) return 0;
175-
return dfs(A, B) || isSubStructure(A->left, B) || isSubStructure(A->right, B);
176-
}
177-
178-
bool dfs(TreeNode* A, TreeNode* B) {
179-
if (!B) return 1;
180-
if (!A || A->val != B->val) return 0;
181-
return dfs(A->left, B->left) && dfs(A->right, B->right);
184+
/**
185+
* @param {TreeNode} A
186+
* @param {TreeNode} B
187+
* @return {boolean}
188+
*/
189+
var isSubStructure = function (A, B) {
190+
if (!A || !B) {
191+
return false;
182192
}
193+
const dfs = (A, B) => {
194+
if (!B) {
195+
return true;
196+
}
197+
if (!A || A.val !== B.val) {
198+
return false;
199+
}
200+
return dfs(A.left, B.left) && dfs(A.right, B.right);
201+
};
202+
return dfs(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B);
183203
};
184204
```
185205

@@ -201,21 +221,20 @@ public:
201221
*/
202222

203223
function isSubStructure(A: TreeNode | null, B: TreeNode | null): boolean {
204-
if (A == null || B == null) {
224+
if (!A || !B) {
205225
return false;
206226
}
227+
const dfs = (A: TreeNode | null, B: TreeNode | null): boolean => {
228+
if (!B) {
229+
return true;
230+
}
231+
if (!A || A.val !== B.val) {
232+
return false;
233+
}
234+
return dfs(A.left, B.left) && dfs(A.right, B.right);
235+
};
207236
return dfs(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B);
208237
}
209-
210-
function dfs(A: TreeNode | null, B: TreeNode | null) {
211-
if (B == null) {
212-
return true;
213-
}
214-
if (A == null) {
215-
return false;
216-
}
217-
return A.val === B.val && dfs(A.left, B.left) && dfs(A.right, B.right);
218-
}
219238
```
220239

221240
### **Rust**

lcof/面试题26. 树的子结构/Solution.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@
1111
* @return {boolean}
1212
*/
1313
var isSubStructure = function (A, B) {
14-
function dfs(A, B) {
15-
if (!B) return true;
16-
if (!A || A.val != B.val) return false;
17-
return dfs(A.left, B.left) && dfs(A.right, B.right);
14+
if (!A || !B) {
15+
return false;
1816
}
19-
if (!A || !B) return false;
17+
const dfs = (A, B) => {
18+
if (!B) {
19+
return true;
20+
}
21+
if (!A || A.val !== B.val) {
22+
return false;
23+
}
24+
return dfs(A.left, B.left) && dfs(A.right, B.right);
25+
};
2026
return dfs(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B);
2127
};

lcof/面试题26. 树的子结构/Solution.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ def dfs(A, B):
1717

1818
if A is None or B is None:
1919
return False
20-
return (
21-
dfs(A, B)
22-
or self.isSubStructure(A.left, B)
23-
or self.isSubStructure(A.right, B)
24-
)
20+
if dfs(A, B):
21+
return True
22+
return self.isSubStructure(A.left, B) or self.isSubStructure(A.right, B)

lcof/面试题26. 树的子结构/Solution.ts

+10-11
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,17 @@
1313
*/
1414

1515
function isSubStructure(A: TreeNode | null, B: TreeNode | null): boolean {
16-
if (A == null || B == null) {
16+
if (!A || !B) {
1717
return false;
1818
}
19+
const dfs = (A: TreeNode | null, B: TreeNode | null): boolean => {
20+
if (!B) {
21+
return true;
22+
}
23+
if (!A || A.val !== B.val) {
24+
return false;
25+
}
26+
return dfs(A.left, B.left) && dfs(A.right, B.right);
27+
};
1928
return dfs(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B);
2029
}
21-
22-
function dfs(A: TreeNode | null, B: TreeNode | null) {
23-
if (B == null) {
24-
return true;
25-
}
26-
if (A == null) {
27-
return false;
28-
}
29-
return A.val === B.val && dfs(A.left, B.left) && dfs(A.right, B.right);
30-
}

0 commit comments

Comments
 (0)