Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b133444

Browse files
update: 114
1 parent 1d11cea commit b133444

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
6767
| 108 | [Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/) | [JavaScript](./src/convert-sorted-array-to-binary-search-tree/res.js) | Easy |
6868
| 110 | [balanced-binary-tree](https://leetcode.com/problems/balanced-binary-tree/) | [TypeScript](./src/balanced-binary-tree/res.ts) | Easy |
6969
| 111 | [Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/) | [JavaScript](./src/minimum-depth-of-binary-tree/res.js) | Easy |
70+
| 114 | [flatten-binary-tree-to-linked-list](https://leetcode.com/problems/flatten-binary-tree-to-linked-list/) | [JavaScript](./src/flatten-binary-tree-to-linked-list/res.ts) | Medium |
7071
| 120 | [Triangle](https://leetcode.com/problems/triangle/) | [JavaScript](./src/triangle/res.js) | Medium |
7172
| 121 | [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) | [JavaScript](./src/best-time-to-buy-and-sell-stock/res.js) | Easy |
7273
| 122 | [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) | [JavaScript](./src/best-time-to-buy-and-sell-stock-ii/res.js) | Easy |
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Definition for a binary tree node.
3+
*/
4+
class TreeNode {
5+
val: number
6+
left: TreeNode | null
7+
right: TreeNode | null
8+
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
9+
this.val = (val===undefined ? 0 : val)
10+
this.left = (left===undefined ? null : left)
11+
this.right = (right===undefined ? null : right)
12+
}
13+
}
14+
15+
/**
16+
Do not return anything, modify root in-place instead.
17+
*/
18+
function flatten(root: TreeNode | null): void {
19+
if (!root) {
20+
return ;
21+
}
22+
23+
const right = root.right;
24+
25+
// 左子树
26+
if (root.left) {
27+
flatten(root.left);
28+
29+
// flatten 后根结点
30+
let currentNode = root.left;
31+
while (currentNode.right) {
32+
currentNode = currentNode.right;
33+
}
34+
35+
// 转移结构
36+
root.right = root.left;
37+
currentNode.right = right;
38+
root.left = null;
39+
}
40+
41+
if (right) {
42+
flatten(right);
43+
}
44+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /