Skip to content

Commit b4c3193

Browse files
committedJul 19, 2020
feat: add golang solution for lcof problems 26-28
1 parent fff2695 commit b4c3193

File tree

6 files changed

+114
-0
lines changed

6 files changed

+114
-0
lines changed
 

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

+25
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,32 @@ var isSubStructure = function(A, B) {
161161
};
162162
```
163163

164+
### Go
165+
166+
```go
167+
func isSubStructure(A *TreeNode, B *TreeNode) bool {
168+
//约定空树不是任意一个树的子结构
169+
if A == nil || B == nil {
170+
return false
171+
}
172+
return helper(A,B) || isSubStructure(A.Left,B) || isSubStructure(A.Right,B)
173+
}
174+
175+
func helper(a *TreeNode, b *TreeNode) bool {
176+
if b == nil {
177+
return true
178+
}
179+
if a == nil {
180+
return false
181+
}
182+
return a.Val == b.Val && helper(a.Left, b.Left) && helper(a.Right, b.Right)
183+
}
184+
```
185+
186+
187+
164188
### ...
189+
165190
```
166191
167192
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func isSubStructure(A *TreeNode, B *TreeNode) bool {
2+
//约定空树不是任意一个树的子结构
3+
if A == nil || B == nil {
4+
return false
5+
}
6+
return helper(A,B) || isSubStructure(A.Left,B) || isSubStructure(A.Right,B)
7+
}
8+
9+
func helper(a *TreeNode, b *TreeNode) bool {
10+
if b == nil {
11+
return true
12+
}
13+
if a == nil {
14+
return false
15+
}
16+
return a.Val == b.Val && helper(a.Left, b.Left) && helper(a.Right, b.Right)
17+
}

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

+17
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,24 @@ var mirrorTree = function(root) {
107107
};
108108
```
109109

110+
### Go
111+
112+
```go
113+
func mirrorTree(root *TreeNode) *TreeNode {
114+
if root == nil {
115+
return root
116+
}
117+
root.Left, root.Right = root.Right, root.Left
118+
root.Left = mirrorTree(root.Left)
119+
root.Right = mirrorTree(root.Right)
120+
return root
121+
}
122+
```
123+
124+
125+
110126
### ...
127+
111128
```
112129
113130
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
func mirrorTree(root *TreeNode) *TreeNode {
2+
if root == nil {
3+
return root
4+
}
5+
root.Left, root.Right = root.Right, root.Left
6+
root.Left = mirrorTree(root.Left)
7+
root.Right = mirrorTree(root.Right)
8+
return root
9+
}

‎lcof/面试题28. 对称的二叉树/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,34 @@ var isSymmetric = function(root) {
120120
};
121121
```
122122

123+
### Go
124+
125+
```go
126+
func isSymmetric(root *TreeNode) bool {
127+
if root == nil {
128+
return true
129+
}
130+
return isSymme(root.Left, root.Right)
131+
}
132+
133+
func isSymme(a *TreeNode, b *TreeNode) bool {
134+
if a == nil && b == nil {
135+
return true
136+
}
137+
if a == nil || b ==nil {
138+
return false
139+
}
140+
if a.Val != b.Val {
141+
return false
142+
}
143+
return isSymme(a.Left,b.Right) && isSymme(a.Right, b.Left)
144+
}
145+
```
146+
147+
148+
123149
### ...
150+
124151
```
125152
126153
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
func isSymmetric(root *TreeNode) bool {
2+
if root == nil {
3+
return true
4+
}
5+
return isSymme(root.Left, root.Right)
6+
}
7+
8+
func isSymme(a *TreeNode, b *TreeNode) bool {
9+
if a == nil && b == nil {
10+
return true
11+
}
12+
if a == nil || b ==nil {
13+
return false
14+
}
15+
if a.Val != b.Val {
16+
return false
17+
}
18+
return isSymme(a.Left,b.Right) && isSymme(a.Right, b.Left)
19+
}

0 commit comments

Comments
 (0)