Skip to content

Commit 8daf86b

Browse files
committed
feat: update solutions to lcof problem: No.26
剑指 Offer 26. 树的子结构
1 parent a483dd4 commit 8daf86b

File tree

6 files changed

+100
-146
lines changed

6 files changed

+100
-146
lines changed

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

+54-74
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,16 @@ B 是 A 的子结构, 即 A 中有出现和 B 相同的结构和节点值。
6262

6363
class Solution:
6464
def isSubStructure(self, A: TreeNode, B: TreeNode) -> bool:
65-
def sub(A, B):
66-
"""判断从当前A节点开始,是否包含B"""
65+
def dfs(A, B):
6766
if B is None:
6867
return True
69-
if A is None:
68+
if A is None or A.val != B.val:
7069
return False
71-
return A.val == B.val and sub(A.left, B.left) and sub(A.right, B.right)
72-
if B is None or A is None:
70+
return dfs(A.left, B.left) and dfs(A.right, B.right)
71+
72+
if A is None or B is None:
7373
return False
74-
if A.val != B.val:
75-
return self.isSubStructure(A.left, B) or self.isSubStructure(A.right, B)
76-
return sub(A, B) or self.isSubStructure(A.left, B) or self.isSubStructure(A.right, B)
74+
return dfs(A, B) or self.isSubStructure(A.left, B) or self.isSubStructure(A.right, B)
7775
```
7876

7977
### **Java**
@@ -90,16 +88,20 @@ class Solution:
9088
*/
9189
class Solution {
9290
public boolean isSubStructure(TreeNode A, TreeNode B) {
93-
if (B == null || A == null) return false;
94-
if (A.val != B.val) return isSubStructure(A.left, B) || isSubStructure(A.right, B);
95-
return sub(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B);
91+
if (A == null || B == null) {
92+
return false;
93+
}
94+
return dfs(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B);
9695
}
9796

98-
private boolean sub(TreeNode A, TreeNode B) {
99-
// 判断从当前A节点开始,是否包含B
100-
if (B == null) return true;
101-
if (A == null) return false;
102-
return A.val == B.val && sub(A.left, B.left) && sub(A.right, B.right);
97+
private boolean dfs(TreeNode A, TreeNode B) {
98+
if (B == null) {
99+
return true;
100+
}
101+
if (A == null || A.val != B.val) {
102+
return false;
103+
}
104+
return dfs(A.left, B.left) && dfs(A.right, B.right);
103105
}
104106
}
105107
```
@@ -119,16 +121,14 @@ class Solution {
119121
* @param {TreeNode} B
120122
* @return {boolean}
121123
*/
122-
var isSubStructure = function (A, B) {
123-
function sub(A, B) {
124+
var isSubStructure = function(A, B) {
125+
function dfs(A, B) {
124126
if (!B) return true;
125-
if (!A) return false;
126-
return A.val == B.val && sub(A.left, B.left) && sub(A.right, B.right);
127+
if (!A || A.val != B.val) return false;
128+
return dfs(A.left, B.left) && dfs(A.right, B.right);
127129
}
128-
if (!B || !A) return false;
129-
if (A.val != B.val)
130-
return isSubStructure(A.left, B) || isSubStructure(A.right, B);
131-
return sub(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B);
130+
if (!A || !B) return false;
131+
return dfs(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B);
132132
};
133133
```
134134

@@ -144,66 +144,46 @@ var isSubStructure = function (A, B) {
144144
* }
145145
*/
146146
func isSubStructure(A *TreeNode, B *TreeNode) bool {
147-
// 约定空树不是任意一个树的子结构
148-
if A == nil || B == nil {
149-
return false
150-
}
151-
return helper(A,B) || isSubStructure(A.Left,B) || isSubStructure(A.Right,B)
152-
}
153-
154-
func helper(a *TreeNode, b *TreeNode) bool {
155-
if b == nil {
156-
return true
157-
}
158-
if a == nil {
159-
return false
160-
}
161-
return a.Val == b.Val && helper(a.Left, b.Left) && helper(a.Right, b.Right)
147+
var dfs func(A, B *TreeNode) bool
148+
dfs = func(A, B *TreeNode) bool {
149+
if B == nil {
150+
return true
151+
}
152+
if A == nil || A.Val != B.Val {
153+
return false
154+
}
155+
return dfs(A.Left, B.Left) && dfs(A.Right, B.Right)
156+
}
157+
if A == nil || B == nil {
158+
return false
159+
}
160+
return dfs(A, B) || isSubStructure(A.Left, B) || isSubStructure(A.Right, B)
162161
}
163162
```
164163

165164
### **C++**
166165

167166
```cpp
167+
/**
168+
* Definition for a binary tree node.
169+
* struct TreeNode {
170+
* int val;
171+
* TreeNode *left;
172+
* TreeNode *right;
173+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
174+
* };
175+
*/
168176
class Solution {
169177
public:
170-
bool isSubTree(TreeNode* a, TreeNode* b) {
171-
if (nullptr == b) {
172-
// 如果小树走到头,则表示ok了
173-
return true;
174-
}
175-
176-
if (nullptr == a) {
177-
// 如果大树走到头,小树却没走到头,说明不对了
178-
return false;
179-
}
180-
181-
if (a->val != b->val) {
182-
return false;
183-
}
184-
185-
return isSubTree(a->left, b->left) && isSubTree(a->right, b->right);
178+
bool isSubStructure(TreeNode* A, TreeNode* B) {
179+
if (!A || !B) return 0;
180+
return dfs(A, B) || isSubStructure(A->left, B) || isSubStructure(A->right, B);
186181
}
187182

188-
bool isSubStructure(TreeNode* a, TreeNode* b) {
189-
bool ret = false;
190-
if (nullptr != a && nullptr != b) {
191-
// 题目约定,空树不属于任何一个数的子树
192-
if (a->val == b->val) {
193-
// 如果值相等,才进入判定
194-
ret = isSubTree(a, b);
195-
}
196-
197-
if (false == ret) {
198-
ret = isSubStructure(a->left, b);
199-
}
200-
201-
if (false == ret) {
202-
ret = isSubStructure(a->right, b);
203-
}
204-
}
205-
206-
return ret;
183+
bool dfs(TreeNode* A, TreeNode* B) {
184+
if (!B) return 1;
185+
if (!A || A->val != B->val) return 0;
186+
return dfs(A->left, B->left) && dfs(A->right, B->right);
207187
}
208188
};
209189
```

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

+7-34
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,14 @@
99
*/
1010
class Solution {
1111
public:
12-
bool isSubTree(TreeNode* a, TreeNode* b) {
13-
if (nullptr == b) {
14-
// 如果小树走到头,则表示ok了
15-
return true;
16-
}
17-
18-
if (nullptr == a) {
19-
// 如果大树走到头,小树却没走到头,说明不对了
20-
return false;
21-
}
22-
23-
if (a->val != b->val) {
24-
return false;
25-
}
26-
27-
return isSubTree(a->left, b->left) && isSubTree(a->right, b->right);
12+
bool isSubStructure(TreeNode* A, TreeNode* B) {
13+
if (!A || !B) return 0;
14+
return dfs(A, B) || isSubStructure(A->left, B) || isSubStructure(A->right, B);
2815
}
2916

30-
bool isSubStructure(TreeNode* a, TreeNode* b) {
31-
bool ret = false;
32-
if (nullptr != a && nullptr != b) {
33-
if (a->val == b->val) {
34-
// 如果值相等,才进入判定
35-
ret = isSubTree(a, b);
36-
}
37-
38-
if (false == ret) {
39-
ret = isSubStructure(a->left, b);
40-
}
41-
42-
if (false == ret) {
43-
ret = isSubStructure(a->right, b);
44-
}
45-
}
46-
47-
return ret;
17+
bool dfs(TreeNode* A, TreeNode* B) {
18+
if (!B) return 1;
19+
if (!A || A->val != B->val) return 0;
20+
return dfs(A->left, B->left) && dfs(A->right, B->right);
4821
}
4922
};

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

+14-15
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,18 @@
77
* }
88
*/
99
func isSubStructure(A *TreeNode, B *TreeNode) bool {
10-
// 约定空树不是任意一个树的子结构
11-
if A == nil || B == nil {
12-
return false
13-
}
14-
return helper(A,B) || isSubStructure(A.Left,B) || isSubStructure(A.Right,B)
15-
}
16-
17-
func helper(a *TreeNode, b *TreeNode) bool {
18-
if b == nil {
19-
return true
20-
}
21-
if a == nil {
22-
return false
23-
}
24-
return a.Val == b.Val && helper(a.Left, b.Left) && helper(a.Right, b.Right)
10+
var dfs func(A, B *TreeNode) bool
11+
dfs = func(A, B *TreeNode) bool {
12+
if B == nil {
13+
return true
14+
}
15+
if A == nil || A.Val != B.Val {
16+
return false
17+
}
18+
return dfs(A.Left, B.Left) && dfs(A.Right, B.Right)
19+
}
20+
if A == nil || B == nil {
21+
return false
22+
}
23+
return dfs(A, B) || isSubStructure(A.Left, B) || isSubStructure(A.Right, B)
2524
}

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

+12-7
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,19 @@
99
*/
1010
class Solution {
1111
public boolean isSubStructure(TreeNode A, TreeNode B) {
12-
if (B == null || A == null) return false;
13-
if (A.val != B.val) return isSubStructure(A.left, B) || isSubStructure(A.right, B);
14-
return sub(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B);
12+
if (A == null || B == null) {
13+
return false;
14+
}
15+
return dfs(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B);
1516
}
1617

17-
private boolean sub(TreeNode A, TreeNode B) {
18-
if (B == null) return true;
19-
if (A == null) return false;
20-
return A.val == B.val && sub(A.left, B.left) && sub(A.right, B.right);
18+
private boolean dfs(TreeNode A, TreeNode B) {
19+
if (B == null) {
20+
return true;
21+
}
22+
if (A == null || A.val != B.val) {
23+
return false;
24+
}
25+
return dfs(A.left, B.left) && dfs(A.right, B.right);
2126
}
2227
}

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

+7-9
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@
1010
* @param {TreeNode} B
1111
* @return {boolean}
1212
*/
13-
var isSubStructure = function (A, B) {
14-
function sub(A, B) {
13+
var isSubStructure = function(A, B) {
14+
function dfs(A, B) {
1515
if (!B) return true;
16-
if (!A) return false;
17-
return A.val == B.val && sub(A.left, B.left) && sub(A.right, B.right);
16+
if (!A || A.val != B.val) return false;
17+
return dfs(A.left, B.left) && dfs(A.right, B.right);
1818
}
19-
if (!B || !A) return false;
20-
if (A.val != B.val)
21-
return isSubStructure(A.left, B) || isSubStructure(A.right, B);
22-
return sub(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B);
23-
};
19+
if (!A || !B) return false;
20+
return dfs(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B);
21+
};

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77

88
class Solution:
99
def isSubStructure(self, A: TreeNode, B: TreeNode) -> bool:
10-
def sub(A, B):
10+
def dfs(A, B):
1111
if B is None:
1212
return True
13-
if A is None:
13+
if A is None or A.val != B.val:
1414
return False
15-
return A.val == B.val and sub(A.left, B.left) and sub(A.right, B.right)
16-
if B is None or A is None:
15+
return dfs(A.left, B.left) and dfs(A.right, B.right)
16+
17+
if A is None or B is None:
1718
return False
18-
if A.val != B.val:
19-
return self.isSubStructure(A.left, B) or self.isSubStructure(A.right, B)
20-
return sub(A, B) or self.isSubStructure(A.left, B) or self.isSubStructure(A.right, B)
19+
return dfs(A, B) or self.isSubStructure(A.left, B) or self.isSubStructure(A.right, B)

0 commit comments

Comments
 (0)