Skip to content

Commit 6cfcaa7

Browse files
committed
feat: add python and java solution to lcof problem
添加《剑指 Offer》题解:面试题26. 树的子结构
1 parent 085a3b5 commit 6cfcaa7

File tree

3 files changed

+188
-0
lines changed

3 files changed

+188
-0
lines changed
+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# [面试题26. 树的子结构](https://leetcode-cn.com/problems/shu-de-zi-jie-gou-lcof/)
2+
3+
## 题目描述
4+
输入两棵二叉树 A 和 B,判断 B 是不是 A W的子结构。(约定空树不是任意一个树的子结构)
5+
6+
B 是 A 的子结构, 即 A 中有出现和 B 相同的结构和节点值。
7+
8+
**例如:**
9+
10+
给定的树 A:
11+
12+
```
13+
     3
14+
    / \
15+
   4   5
16+
  / \
17+
 1   2
18+
```
19+
20+
给定的树 B:
21+
22+
```
23+
   4 
24+
  /
25+
 1
26+
```
27+
28+
返回 true,因为 B 与 A 的一个子树拥有相同的结构和节点值。
29+
30+
**示例 1:**
31+
32+
```
33+
输入:A = [1,2,3], B = [3,1]
34+
输出:false
35+
```
36+
37+
**示例 2:**
38+
39+
```
40+
输入:A = [3,4,5,1,2], B = [4,1]
41+
输出:true
42+
```
43+
44+
**限制:**
45+
46+
- `0 <= 节点个数 <= 10000`
47+
48+
## 解法
49+
### Python3
50+
```python
51+
# Definition for a binary tree node.
52+
# class TreeNode:
53+
# def __init__(self, x):
54+
# self.val = x
55+
# self.left = None
56+
# self.right = None
57+
58+
class Solution:
59+
def isSubStructure(self, A: TreeNode, B: TreeNode) -> bool:
60+
return self.sub(A, B) if B else False
61+
62+
def sub(self, A: TreeNode, B: TreeNode) -> bool:
63+
if B is None:
64+
return True
65+
if A is None:
66+
return False
67+
if A.val == B.val:
68+
return self.same(A, B) or self.sub(A.left, B) or self.sub(A.right, B)
69+
return self.sub(A.left, B) or self.sub(A.right, B)
70+
71+
def same(self, A: TreeNode, B: TreeNode) -> bool:
72+
if B is None:
73+
return True
74+
if A is None or A.val != B.val:
75+
return False
76+
return self.same(A.left, B.left) and self.same(A.right, B.right)
77+
```
78+
79+
### Java
80+
```java
81+
/**
82+
* Definition for a binary tree node.
83+
* public class TreeNode {
84+
* int val;
85+
* TreeNode left;
86+
* TreeNode right;
87+
* TreeNode(int x) { val = x; }
88+
* }
89+
*/
90+
class Solution {
91+
public boolean isSubStructure(TreeNode A, TreeNode B) {
92+
return B == null ? false : sub(A, B);
93+
}
94+
95+
private boolean sub(TreeNode A, TreeNode B) {
96+
if (B == null) {
97+
return true;
98+
}
99+
if (A == null) {
100+
return false;
101+
}
102+
if (A.val == B.val) {
103+
return isSame(A, B) || sub(A.left, B) || sub(A.right, B);
104+
}
105+
return sub(A.left, B) || sub(A.right, B);
106+
107+
}
108+
109+
private boolean isSame(TreeNode A, TreeNode B) {
110+
if (B == null) {
111+
return true;
112+
}
113+
if (A == null || A.val != B.val) {
114+
return false;
115+
}
116+
return isSame(A.left, B.left) && isSame(A.right, B.right);
117+
}
118+
}
119+
```
120+
121+
### ...
122+
```
123+
124+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public boolean isSubStructure(TreeNode A, TreeNode B) {
12+
return B == null ? false : sub(A, B);
13+
}
14+
15+
private boolean sub(TreeNode A, TreeNode B) {
16+
if (B == null) {
17+
return true;
18+
}
19+
if (A == null) {
20+
return false;
21+
}
22+
if (A.val == B.val) {
23+
return isSame(A, B) || sub(A.left, B) || sub(A.right, B);
24+
}
25+
return sub(A.left, B) || sub(A.right, B);
26+
27+
}
28+
29+
private boolean isSame(TreeNode A, TreeNode B) {
30+
if (B == null) {
31+
return true;
32+
}
33+
if (A == null || A.val != B.val) {
34+
return false;
35+
}
36+
return isSame(A.left, B.left) && isSame(A.right, B.right);
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution:
9+
def isSubStructure(self, A: TreeNode, B: TreeNode) -> bool:
10+
return self.sub(A, B) if B else False
11+
12+
def sub(self, A: TreeNode, B: TreeNode) -> bool:
13+
if B is None:
14+
return True
15+
if A is None:
16+
return False
17+
if A.val == B.val:
18+
return self.same(A, B) or self.sub(A.left, B) or self.sub(A.right, B)
19+
return self.sub(A.left, B) or self.sub(A.right, B)
20+
21+
def same(self, A: TreeNode, B: TreeNode) -> bool:
22+
if B is None:
23+
return True
24+
if A is None or A.val != B.val:
25+
return False
26+
return self.same(A.left, B.left) and self.same(A.right, B.right)

0 commit comments

Comments
 (0)