File tree 6 files changed +217
-0
lines changed
6 files changed +217
-0
lines changed Original file line number Diff line number Diff line change @@ -110,7 +110,43 @@ var verifyPostorder = function(postorder) {
110
110
};
111
111
```
112
112
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
+
113
148
### ...
149
+
114
150
```
115
151
116
152
```
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -139,7 +139,40 @@ var pathSum = function(root, sum) {
139
139
};
140
140
```
141
141
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
+
142
174
### ...
175
+
143
176
```
144
177
145
178
```
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -155,7 +155,58 @@ var copyRandomList = function(head) {
155
155
};
156
156
```
157
157
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
+
158
208
### ...
209
+
159
210
```
160
211
161
212
```
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments