Skip to content

Commit b079007

Browse files
authored
feat: add typescript solution to lc problem: No.0114.Flatten Binary Tree to Linked List (doocs#456)
1 parent 1f7deff commit b079007

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,42 @@ class Solution {
126126
}
127127
```
128128

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

131167
```cpp

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,42 @@ 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+
112148
### **C++**
113149

114150
```cpp
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
/**
16+
Do not return anything, modify root in-place instead.
17+
*/
18+
function flatten(root: TreeNode | null): void {
19+
while (root != null) {
20+
if (root.left != null) {
21+
let pre = root.left;
22+
while (pre.right != null) {
23+
pre = pre.right;
24+
}
25+
pre.right = root.right;
26+
root.right = root.left;
27+
root.left = null;
28+
}
29+
root = root.right;
30+
}
31+
};

0 commit comments

Comments
 (0)