Skip to content

Commit d0a2816

Browse files
committed
feat: add golang solution for lcof problem 33-35
1 parent ca79c4e commit d0a2816

File tree

6 files changed

+217
-0
lines changed

6 files changed

+217
-0
lines changed

lcof/面试题33. 二叉搜索树的后序遍历序列/README.md

+36
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,43 @@ var verifyPostorder = function(postorder) {
110110
};
111111
```
112112

113+
### Go
114+
115+
```go
116+
func verifyPostorder(postorder []int) bool {
117+
if len(postorder) < 2 {
118+
return true
119+
}
120+
return helper(postorder, 0, len(postorder)-1)
121+
}
122+
//递归
123+
func helper(postorder []int , left,right int) bool {
124+
if left >= right {
125+
return true
126+
}
127+
//最后一位即根
128+
rootValue := postorder[right]
129+
//从左开始往右遍历,直到大于根停止,小于部分是左子树
130+
i := left
131+
for i < right && postorder[i] < rootValue {
132+
i++
133+
}
134+
//剩下部分是右子树,检查是否都大于根值
135+
for j := i; j < right; j++ {
136+
if postorder[j] < rootValue {
137+
return false
138+
}
139+
}
140+
l := helper(postorder,left,i-1) //检查左子树,左子树i要减一
141+
r := helper(postorder,i,right-1)//检查右子树,剔除最后一位是根
142+
return l && r
143+
}
144+
```
145+
146+
147+
113148
### ...
149+
114150
```
115151
116152
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
func verifyPostorder(postorder []int) bool {
2+
if len(postorder) < 2 {
3+
return true
4+
}
5+
return helper(postorder, 0, len(postorder)-1)
6+
}
7+
//递归
8+
func helper(postorder []int , left,right int) bool {
9+
if left >= right {
10+
return true
11+
}
12+
//最后一位即根
13+
rootValue := postorder[right]
14+
//从左开始往右遍历,直到大于根停止,小于部分是左子树
15+
i := left
16+
for i < right && postorder[i] < rootValue {
17+
i++
18+
}
19+
//剩下部分是右子树,检查是否都大于根值
20+
for j := i; j < right; j++ {
21+
if postorder[j] < rootValue {
22+
return false
23+
}
24+
}
25+
l := helper(postorder,left,i-1) //检查左子树,左子树i要减一
26+
r := helper(postorder,i,right-1)//检查右子树,剔除最后一位是根
27+
return l && r
28+
}

lcof/面试题34. 二叉树中和为某一值的路径/README.md

+33
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,40 @@ var pathSum = function(root, sum) {
139139
};
140140
```
141141

142+
### Go
143+
144+
```go
145+
var res [][]int
146+
func pathSum(root *TreeNode, sum int) [][]int {
147+
res = [][]int{}
148+
if root == nil {
149+
return res
150+
}
151+
helper(root, sum, []int{})
152+
return res
153+
}
154+
155+
func helper(node *TreeNode, target int, ans []int) {
156+
if node == nil {
157+
return
158+
}
159+
ans = append(ans,node.Val)
160+
target -= node.Val
161+
if target == 0 && node.Left == nil && node.Right == nil {
162+
tmp := make([]int,len(ans))
163+
copy(tmp,ans)
164+
res = append(res,tmp)
165+
} else {
166+
helper(node.Left, target, ans)
167+
helper(node.Right, target, ans)
168+
}
169+
}
170+
```
171+
172+
173+
142174
### ...
175+
143176
```
144177
145178
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var res [][]int
2+
func pathSum(root *TreeNode, sum int) [][]int {
3+
res = [][]int{}
4+
if root == nil {
5+
return res
6+
}
7+
helper(root, sum, []int{})
8+
return res
9+
}
10+
11+
func helper(node *TreeNode, target int, ans []int) {
12+
if node == nil {
13+
return
14+
}
15+
ans = append(ans,node.Val)
16+
target -= node.Val
17+
if target == 0 && node.Left == nil && node.Right == nil {
18+
tmp := make([]int,len(ans))
19+
copy(tmp,ans)
20+
res = append(res,tmp)
21+
} else {
22+
helper(node.Left, target, ans)
23+
helper(node.Right, target, ans)
24+
}
25+
}

lcof/面试题35. 复杂链表的复制/README.md

+51
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,58 @@ var copyRandomList = function(head) {
155155
};
156156
```
157157

158+
### Go
159+
160+
```go
161+
func copyRandomList(head *Node) *Node {
162+
if head == nil {
163+
return nil
164+
}
165+
cur, next := head,head
166+
//完成对当前节点的复制
167+
for cur != nil {
168+
next = cur.Next
169+
cur.Next = &Node{
170+
Val: cur.Val,
171+
Next: next,
172+
Random: nil,
173+
}
174+
cur = next
175+
}
176+
//设置复制节点的random节点
177+
cur = head
178+
curCopy := head
179+
for cur != nil {
180+
next = cur.Next.Next
181+
curCopy = cur.Next
182+
if cur.Random == nil {
183+
curCopy.Random = nil
184+
} else {
185+
curCopy.Random = cur.Random.Next
186+
}
187+
cur = next
188+
}
189+
res := head.Next
190+
cur = head
191+
for cur != nil {
192+
next = cur.Next.Next
193+
curCopy = cur.Next
194+
cur.Next = next
195+
if next != nil {
196+
curCopy.Next = next.Next
197+
} else {
198+
curCopy.Next = nil
199+
}
200+
cur = cur.Next
201+
}
202+
return res
203+
}
204+
```
205+
206+
207+
158208
### ...
209+
159210
```
160211
161212
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
func copyRandomList(head *Node) *Node {
2+
if head == nil {
3+
return nil
4+
}
5+
cur, next := head,head
6+
//完成对当前节点的复制
7+
for cur != nil {
8+
next = cur.Next
9+
cur.Next = &Node{
10+
Val: cur.Val,
11+
Next: next,
12+
Random: nil,
13+
}
14+
cur = next
15+
}
16+
//设置复制节点的random节点
17+
cur = head
18+
curCopy := head
19+
for cur != nil {
20+
next = cur.Next.Next
21+
curCopy = cur.Next
22+
if cur.Random == nil {
23+
curCopy.Random = nil
24+
} else {
25+
curCopy.Random = cur.Random.Next
26+
}
27+
cur = next
28+
}
29+
res := head.Next
30+
cur = head
31+
for cur != nil {
32+
next = cur.Next.Next
33+
curCopy = cur.Next
34+
cur.Next = next
35+
if next != nil {
36+
curCopy.Next = next.Next
37+
} else {
38+
curCopy.Next = nil
39+
}
40+
cur = cur.Next
41+
}
42+
return res
43+
44+
}

0 commit comments

Comments
 (0)