Skip to content

Commit 95e9936

Browse files
authored
feat: add go solution to lc problem: No.0114 (doocs#842)
No.0114.Flatten Binary Tree to Linked List
1 parent aa5f136 commit 95e9936

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

solution/0100-0199/0114.Flatten Binary Tree to Linked List/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,33 @@ public:
194194
};
195195
```
196196
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+
197224
### **...**
198225

199226
```

solution/0100-0199/0114.Flatten Binary Tree to Linked List/README_EN.md

+27
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,33 @@ public:
178178
};
179179
```
180180
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+
181208
### **...**
182209

183210
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}

0 commit comments

Comments
 (0)