File tree 3 files changed +76
-0
lines changed
solution/0100-0199/0114.Flatten Binary Tree to Linked List
3 files changed +76
-0
lines changed Original file line number Diff line number Diff line change @@ -194,6 +194,33 @@ public:
194
194
};
195
195
```
196
196
197
+ ### **Go**
198
+
199
+ ```go
200
+ /**
201
+ * Definition for a binary tree node.
202
+ * type TreeNode struct {
203
+ * Val int
204
+ * Left *TreeNode
205
+ * Right *TreeNode
206
+ * }
207
+ */
208
+ func flatten(root *TreeNode) {
209
+ for root != nil {
210
+ left, right := root.Left, root.Right
211
+ root.Left = nil
212
+ if left != nil {
213
+ root.Right = left
214
+ for left.Right != nil {
215
+ left = left.Right
216
+ }
217
+ left.Right = right
218
+ }
219
+ root = root.Right
220
+ }
221
+ }
222
+ ```
223
+
197
224
### ** ...**
198
225
199
226
```
Original file line number Diff line number Diff line change @@ -178,6 +178,33 @@ public:
178
178
};
179
179
```
180
180
181
+ ### **Go**
182
+
183
+ ```go
184
+ /**
185
+ * Definition for a binary tree node.
186
+ * type TreeNode struct {
187
+ * Val int
188
+ * Left *TreeNode
189
+ * Right *TreeNode
190
+ * }
191
+ */
192
+ func flatten(root *TreeNode) {
193
+ for root != nil {
194
+ left, right := root.Left, root.Right
195
+ root.Left = nil
196
+ if left != nil {
197
+ root.Right = left
198
+ for left.Right != nil {
199
+ left = left.Right
200
+ }
201
+ left.Right = right
202
+ }
203
+ root = root.Right
204
+ }
205
+ }
206
+ ```
207
+
181
208
### ** ...**
182
209
183
210
```
Original file line number Diff line number Diff line change
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 flatten (root * TreeNode ) {
10
+ for root != nil {
11
+ left , right := root .Left , root .Right
12
+ root .Left = nil
13
+ if left != nil {
14
+ root .Right = left
15
+ for left .Right != nil {
16
+ left = left .Right
17
+ }
18
+ left .Right = right
19
+ }
20
+ root = root .Right
21
+ }
22
+ }
You can’t perform that action at this time.
0 commit comments