Skip to content

Commit d014127

Browse files
committed
feat: add solutions to lc problem: No.0114
No.0114.Flatten Binary Tree to Linked List
1 parent d5e5bef commit d014127

File tree

4 files changed

+221
-75
lines changed

4 files changed

+221
-75
lines changed

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

+101-37
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
**方法一:寻找前驱节点**
57+
58+
先序遍历的访问顺序是“根、左子树、右子树”,左子树最后一个节点访问完后,接着会访问根节点的右子树节点。
59+
60+
因此,对于当前节点,如果其左子节点不为空,我们找到左子树的最右节点,作为前驱节点,然后将当前节点的右子节点赋给前驱节点的右子节点。然后将当前节点的左子节点赋给当前节点的右子节点,并将当前节点的左子节点置为空。然后将当前节点的右子节点作为下一个节点,继续处理,直至所有节点处理完毕。
61+
62+
时间复杂度 $O(n)$,空间复杂度 O(1)$。其中 $n$ 是树中节点的个数。
63+
5664
<!-- tabs:start -->
5765

5866
### **Python3**
@@ -67,7 +75,7 @@
6775
# self.left = left
6876
# self.right = right
6977
class Solution:
70-
def flatten(self, root: TreeNode) -> None:
78+
def flatten(self, root: Optional[TreeNode]) -> None:
7179
"""
7280
Do not return anything, modify root in-place instead.
7381
"""
@@ -125,42 +133,6 @@ class Solution {
125133
}
126134
```
127135

128-
### **TypeScript**
129-
130-
```ts
131-
/**
132-
* Definition for a binary tree node.
133-
* class TreeNode {
134-
* val: number
135-
* left: TreeNode | null
136-
* right: TreeNode | null
137-
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
138-
* this.val = (val===undefined ? 0 : val)
139-
* this.left = (left===undefined ? null : left)
140-
* this.right = (right===undefined ? null : right)
141-
* }
142-
* }
143-
*/
144-
145-
/**
146-
Do not return anything, modify root in-place instead.
147-
*/
148-
function flatten(root: TreeNode | null): void {
149-
while (root != null) {
150-
if (root.left != null) {
151-
let pre = root.left;
152-
while (pre.right != null) {
153-
pre = pre.right;
154-
}
155-
pre.right = root.right;
156-
root.right = root.left;
157-
root.left = null;
158-
}
159-
root = root.right;
160-
}
161-
}
162-
```
163-
164136
### **C++**
165137

166138
```cpp
@@ -196,6 +168,31 @@ public:
196168
197169
### **Go**
198170
171+
```go
172+
/**
173+
* Definition for a binary tree node.
174+
* type TreeNode struct {
175+
* Val int
176+
* Left *TreeNode
177+
* Right *TreeNode
178+
* }
179+
*/
180+
func flatten(root *TreeNode) {
181+
for root != nil {
182+
if root.Left != nil {
183+
pre := root.Left
184+
for pre.Right != nil {
185+
pre = pre.Right
186+
}
187+
pre.Right = root.Right
188+
root.Right = root.Left
189+
root.Left = nil
190+
}
191+
root = root.Right
192+
}
193+
}
194+
```
195+
199196
```go
200197
/**
201198
* Definition for a binary tree node.
@@ -221,6 +218,73 @@ func flatten(root *TreeNode) {
221218
}
222219
```
223220

221+
### **TypeScript**
222+
223+
```ts
224+
/**
225+
* Definition for a binary tree node.
226+
* class TreeNode {
227+
* val: number
228+
* left: TreeNode | null
229+
* right: TreeNode | null
230+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
231+
* this.val = (val===undefined ? 0 : val)
232+
* this.left = (left===undefined ? null : left)
233+
* this.right = (right===undefined ? null : right)
234+
* }
235+
* }
236+
*/
237+
238+
/**
239+
Do not return anything, modify root in-place instead.
240+
*/
241+
function flatten(root: TreeNode | null): void {
242+
while (root != null) {
243+
if (root.left != null) {
244+
let pre = root.left;
245+
while (pre.right != null) {
246+
pre = pre.right;
247+
}
248+
pre.right = root.right;
249+
root.right = root.left;
250+
root.left = null;
251+
}
252+
root = root.right;
253+
}
254+
}
255+
```
256+
257+
### **JavaScript**
258+
259+
```js
260+
/**
261+
* Definition for a binary tree node.
262+
* function TreeNode(val, left, right) {
263+
* this.val = (val===undefined ? 0 : val)
264+
* this.left = (left===undefined ? null : left)
265+
* this.right = (right===undefined ? null : right)
266+
* }
267+
*/
268+
/**
269+
* @param {TreeNode} root
270+
* @return {void} Do not return anything, modify root in-place instead.
271+
*/
272+
var flatten = function (root) {
273+
while (root) {
274+
if (root.left) {
275+
let pre = root.left;
276+
while (pre.right) {
277+
pre = pre.right;
278+
}
279+
pre.right = root.right;
280+
root.right = root.left;
281+
root.left = null;
282+
}
283+
root = root.right;
284+
}
285+
};
286+
```
287+
224288
### **...**
225289

226290
```

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

+93-37
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
# self.left = left
5959
# self.right = right
6060
class Solution:
61-
def flatten(self, root: TreeNode) -> None:
61+
def flatten(self, root: Optional[TreeNode]) -> None:
6262
"""
6363
Do not return anything, modify root in-place instead.
6464
"""
@@ -109,42 +109,6 @@ class Solution {
109109
}
110110
```
111111

112-
### **TypeScript**
113-
114-
```ts
115-
/**
116-
* Definition for a binary tree node.
117-
* class TreeNode {
118-
* val: number
119-
* left: TreeNode | null
120-
* right: TreeNode | null
121-
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
122-
* this.val = (val===undefined ? 0 : val)
123-
* this.left = (left===undefined ? null : left)
124-
* this.right = (right===undefined ? null : right)
125-
* }
126-
* }
127-
*/
128-
129-
/**
130-
Do not return anything, modify root in-place instead.
131-
*/
132-
function flatten(root: TreeNode | null): void {
133-
while (root != null) {
134-
if (root.left != null) {
135-
let pre = root.left;
136-
while (pre.right != null) {
137-
pre = pre.right;
138-
}
139-
pre.right = root.right;
140-
root.right = root.left;
141-
root.left = null;
142-
}
143-
root = root.right;
144-
}
145-
}
146-
```
147-
148112
### **C++**
149113

150114
```cpp
@@ -180,6 +144,31 @@ public:
180144
181145
### **Go**
182146
147+
```go
148+
/**
149+
* Definition for a binary tree node.
150+
* type TreeNode struct {
151+
* Val int
152+
* Left *TreeNode
153+
* Right *TreeNode
154+
* }
155+
*/
156+
func flatten(root *TreeNode) {
157+
for root != nil {
158+
if root.Left != nil {
159+
pre := root.Left
160+
for pre.Right != nil {
161+
pre = pre.Right
162+
}
163+
pre.Right = root.Right
164+
root.Right = root.Left
165+
root.Left = nil
166+
}
167+
root = root.Right
168+
}
169+
}
170+
```
171+
183172
```go
184173
/**
185174
* Definition for a binary tree node.
@@ -205,6 +194,73 @@ func flatten(root *TreeNode) {
205194
}
206195
```
207196

197+
### **TypeScript**
198+
199+
```ts
200+
/**
201+
* Definition for a binary tree node.
202+
* class TreeNode {
203+
* val: number
204+
* left: TreeNode | null
205+
* right: TreeNode | null
206+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
207+
* this.val = (val===undefined ? 0 : val)
208+
* this.left = (left===undefined ? null : left)
209+
* this.right = (right===undefined ? null : right)
210+
* }
211+
* }
212+
*/
213+
214+
/**
215+
Do not return anything, modify root in-place instead.
216+
*/
217+
function flatten(root: TreeNode | null): void {
218+
while (root != null) {
219+
if (root.left != null) {
220+
let pre = root.left;
221+
while (pre.right != null) {
222+
pre = pre.right;
223+
}
224+
pre.right = root.right;
225+
root.right = root.left;
226+
root.left = null;
227+
}
228+
root = root.right;
229+
}
230+
}
231+
```
232+
233+
### **JavaScript**
234+
235+
```js
236+
/**
237+
* Definition for a binary tree node.
238+
* function TreeNode(val, left, right) {
239+
* this.val = (val===undefined ? 0 : val)
240+
* this.left = (left===undefined ? null : left)
241+
* this.right = (right===undefined ? null : right)
242+
* }
243+
*/
244+
/**
245+
* @param {TreeNode} root
246+
* @return {void} Do not return anything, modify root in-place instead.
247+
*/
248+
var flatten = function (root) {
249+
while (root) {
250+
if (root.left) {
251+
let pre = root.left;
252+
while (pre.right) {
253+
pre = pre.right;
254+
}
255+
pre.right = root.right;
256+
root.right = root.left;
257+
root.left = null;
258+
}
259+
root = root.right;
260+
}
261+
};
262+
```
263+
208264
### **...**
209265

210266
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @return {void} Do not return anything, modify root in-place instead.
12+
*/
13+
var flatten = function (root) {
14+
while (root) {
15+
if (root.left) {
16+
let pre = root.left;
17+
while (pre.right) {
18+
pre = pre.right;
19+
}
20+
pre.right = root.right;
21+
root.right = root.left;
22+
root.left = null;
23+
}
24+
root = root.right;
25+
}
26+
};

solution/0100-0199/0114.Flatten Binary Tree to Linked List/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# self.left = left
66
# self.right = right
77
class Solution:
8-
def flatten(self, root: TreeNode) -> None:
8+
def flatten(self, root: Optional[TreeNode]) -> None:
99
"""
1010
Do not return anything, modify root in-place instead.
1111
"""

0 commit comments

Comments
 (0)