Skip to content

Commit a863e7f

Browse files
committed
feat: add solutions to leetcode and lcof problems
1 parent 0bc45ea commit a863e7f

File tree

18 files changed

+330
-178
lines changed

18 files changed

+330
-178
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@
8686
### 二叉树
8787

8888
1. [对称二叉树](/solution/0100-0199/0101.Symmetric%20Tree/README.md)
89+
1. [树的子结构](/lcof/面试题26.%20树的子结构/README.md)
90+
1. [翻转二叉树](/solution/0200-0299/0226.Invert%20Binary%20Tree/README.md)
8991
1. [二叉树的层次遍历](/solution/0100-0199/0102.Binary%20Tree%20Level%20Order%20Traversal/README.md)
9092
1. [二叉树的层次遍历 II](/solution/0100-0199/0107.Binary%20Tree%20Level%20Order%20Traversal%20II/README.md)
9193
1. [二叉树的最大深度](/solution/0100-0199/0104.Maximum%20Depth%20of%20Binary%20Tree/README.md)

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
8484
### Binary Tree
8585

8686
1. [Symmetric Tree](/solution/0100-0199/0101.Symmetric%20Tree/README_EN.md)
87+
1. [Invert Binary Tree](/solution/0200-0299/0226.Invert%20Binary%20Tree/README_EN.md)
8788
1. [Binary Tree Level Order Traversal](/solution/0100-0199/0102.Binary%20Tree%20Level%20Order%20Traversal/README_EN.md)
8889
1. [Binary Tree Level Order Traversal II](/solution/0100-0199/0107.Binary%20Tree%20Level%20Order%20Traversal%20II/README_EN.md)
8990
1. [Maximum Depth of Binary Tree](/solution/0100-0199/0104.Maximum%20Depth%20of%20Binary%20Tree/README_EN.md)

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

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

6363
class Solution:
6464
def isSubStructure(self, A: TreeNode, B: TreeNode) -> bool:
65-
return self.sub(A, B) if B else False
66-
67-
def sub(self, A: TreeNode, B: TreeNode) -> bool:
68-
if B is None:
69-
return True
70-
if A is None:
71-
return False
72-
if A.val == B.val:
73-
return self.same(A, B) or self.sub(A.left, B) or self.sub(A.right, B)
74-
return self.sub(A.left, B) or self.sub(A.right, B)
75-
76-
def same(self, A: TreeNode, B: TreeNode) -> bool:
77-
if B is None:
78-
return True
79-
if A is None or A.val != B.val:
65+
def sub(A, B):
66+
"""判断从当前A节点开始,是否包含B"""
67+
if B is None:
68+
return True
69+
if A is None:
70+
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:
8073
return False
81-
return self.same(A.left, B.left) and self.same(A.right, B.right)
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)
8277
```
8378

8479
### **Java**
@@ -95,31 +90,16 @@ class Solution:
9590
*/
9691
class Solution {
9792
public boolean isSubStructure(TreeNode A, TreeNode B) {
98-
return B == null ? false : sub(A, 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);
9996
}
10097

10198
private boolean sub(TreeNode A, TreeNode B) {
102-
if (B == null) {
103-
return true;
104-
}
105-
if (A == null) {
106-
return false;
107-
}
108-
if (A.val == B.val) {
109-
return isSame(A, B) || sub(A.left, B) || sub(A.right, B);
110-
}
111-
return sub(A.left, B) || sub(A.right, B);
112-
113-
}
114-
115-
private boolean isSame(TreeNode A, TreeNode B) {
116-
if (B == null) {
117-
return true;
118-
}
119-
if (A == null || A.val != B.val) {
120-
return false;
121-
}
122-
return isSame(A.left, B.left) && isSame(A.right, B.right);
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);
123103
}
124104
}
125105
```
@@ -140,39 +120,31 @@ class Solution {
140120
* @return {boolean}
141121
*/
142122
var isSubStructure = function (A, B) {
143-
if (!B || !A) return false;
144-
let res;
145-
function dfs(A, B, bool) {
146-
if (!A || !B) {
147-
if (B) {
148-
return false;
149-
} else {
150-
return true;
151-
}
152-
}
153-
if (A.val === B.val) {
154-
let left = dfs(A.left, B.left, true);
155-
let right = dfs(A.right, B.right, true);
156-
if (left && right) return true;
157-
else return false;
158-
} else {
159-
if (bool) return false;
160-
else {
161-
let left = dfs(A.left, B, false);
162-
let right = dfs(A.right, B, false);
163-
return left || right;
164-
}
165-
}
123+
function sub(A, B) {
124+
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);
166127
}
167-
return dfs(A, B, false) || false;
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);
168132
};
169133
```
170134

171135
### **Go**
172136

173137
```go
138+
/**
139+
* Definition for a binary tree node.
140+
* type TreeNode struct {
141+
* Val int
142+
* Left *TreeNode
143+
* Right *TreeNode
144+
* }
145+
*/
174146
func isSubStructure(A *TreeNode, B *TreeNode) bool {
175-
//约定空树不是任意一个树的子结构
147+
// 约定空树不是任意一个树的子结构
176148
if A == nil || B == nil {
177149
return false
178150
}

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
func isSubStructure(A *TreeNode, B *TreeNode) bool {
2-
//约定空树不是任意一个树的子结构
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 isSubStructure(A *TreeNode, B *TreeNode) bool {
10+
// 约定空树不是任意一个树的子结构
311
if A == nil || B == nil {
412
return false
513
}

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

+6-22
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,14 @@
99
*/
1010
class Solution {
1111
public boolean isSubStructure(TreeNode A, TreeNode B) {
12-
return B == null ? false : sub(A, 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);
1315
}
1416

1517
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);
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);
3721
}
3822
}

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

+8-24
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,13 @@
1111
* @return {boolean}
1212
*/
1313
var isSubStructure = function (A, B) {
14-
if (!B || !A) return false;
15-
let res;
16-
function dfs(A, B, bool) {
17-
if (!A || !B) {
18-
if (B) {
19-
return false;
20-
} else {
21-
return true;
22-
}
23-
}
24-
if (A.val === B.val) {
25-
let left = dfs(A.left, B.left, true);
26-
let right = dfs(A.right, B.right, true);
27-
if (left && right) return true;
28-
else return false;
29-
} else {
30-
if (bool) return false;
31-
else {
32-
let left = dfs(A.left, B, false);
33-
let right = dfs(A.right, B, false);
34-
return left || right;
35-
}
36-
}
14+
function sub(A, B) {
15+
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);
3718
}
38-
return dfs(A, B, false) || false;
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);
3923
};

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

+10-16
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,14 @@
77

88
class Solution:
99
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:
10+
def sub(A, B):
11+
if B is None:
12+
return True
13+
if A is None:
14+
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:
2517
return False
26-
return self.same(A.left, B.left) and self.same(A.right, B.right)
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)

lcof/面试题27. 二叉树的镜像/README.md

+21-20
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,11 @@
5151

5252
class Solution:
5353
def mirrorTree(self, root: TreeNode) -> TreeNode:
54-
if root is None or (root.left is None and root.right is None):
55-
return root
56-
54+
if root is None:
55+
return None
56+
root.left, root.right = root.right, root.left
5757
self.mirrorTree(root.left)
5858
self.mirrorTree(root.right)
59-
root.left, root.right = root.right, root.left
6059
return root
6160
```
6261

@@ -74,14 +73,12 @@ class Solution:
7473
*/
7574
class Solution {
7675
public TreeNode mirrorTree(TreeNode root) {
77-
if (root == null || (root.left == null && root.right == null)) {
78-
return root;
79-
}
80-
mirrorTree(root.left);
81-
mirrorTree(root.right);
76+
if (root == null) return null;
8277
TreeNode t = root.left;
8378
root.left = root.right;
8479
root.right = t;
80+
mirrorTree(root.left);
81+
mirrorTree(root.right);
8582
return root;
8683
}
8784
}
@@ -102,28 +99,32 @@ class Solution {
10299
* @return {TreeNode}
103100
*/
104101
var mirrorTree = function (root) {
105-
function dfs(node) {
106-
if (!node) return null;
107-
let left = dfs(node.left);
108-
let right = dfs(node.right);
109-
node.left = right;
110-
node.right = left;
111-
return node;
112-
}
113-
return dfs(root);
102+
if (!root) return null;
103+
[root.left, root.right] = [root.right, root.left];
104+
mirrorTree(root.left);
105+
mirrorTree(root.right);
106+
return root;
114107
};
115108
```
116109

117110
### **Go**
118111

119112
```go
113+
/**
114+
* Definition for a binary tree node.
115+
* type TreeNode struct {
116+
* Val int
117+
* Left *TreeNode
118+
* Right *TreeNode
119+
* }
120+
*/
120121
func mirrorTree(root *TreeNode) *TreeNode {
121122
if root == nil {
122123
return root
123124
}
124125
root.Left, root.Right = root.Right, root.Left
125-
root.Left = mirrorTree(root.Left)
126-
root.Right = mirrorTree(root.Right)
126+
mirrorTree(root.Left)
127+
mirrorTree(root.Right)
127128
return root
128129
}
129130
```
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
func mirrorTree(root *TreeNode) *TreeNode {
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 mirrorTree(root *TreeNode) *TreeNode {
210
if root == nil {
311
return root
412
}
513
root.Left, root.Right = root.Right, root.Left
6-
root.Left = mirrorTree(root.Left)
7-
root.Right = mirrorTree(root.Right)
14+
mirrorTree(root.Left)
15+
mirrorTree(root.Right)
816
return root
917
}

lcof/面试题27. 二叉树的镜像/Solution.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@
99
*/
1010
class Solution {
1111
public TreeNode mirrorTree(TreeNode root) {
12-
if (root == null || (root.left == null && root.right == null)) {
13-
return root;
14-
}
15-
mirrorTree(root.left);
16-
mirrorTree(root.right);
12+
if (root == null) return null;
1713
TreeNode t = root.left;
1814
root.left = root.right;
1915
root.right = t;
16+
mirrorTree(root.left);
17+
mirrorTree(root.right);
2018
return root;
2119
}
2220
}

0 commit comments

Comments
 (0)