Skip to content

Commit 0aaa6e9

Browse files
committed
feat: add solutions to lc problem: No.0114. Flatten Binary Tree to Linked List
1 parent 2a1a8e4 commit 0aaa6e9

File tree

12 files changed

+914
-278
lines changed

12 files changed

+914
-278
lines changed

.docsifytopdfrc.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
contents: ["summary.md"],
3+
pathToPublic: "pdf/doocs-leetcode.pdf",
4+
pdfOptions: "<options for puppeteer.pdf()>",
5+
removeTemp: true,
6+
emulateMedia: "screen",
7+
};

README.md

+127-127
Large diffs are not rendered by default.

README_EN.md

+118-118
Large diffs are not rendered by default.

package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
{
22
"scripts": {
3-
"dev": "docsify serve --open"
3+
"dev": "docsify serve --open",
4+
"convert": "docsify-pdf-converter"
45
},
56
"dependencies": {
67
"docsify-cli": "^4.4.3"
8+
},
9+
"devDependencies": {
10+
"docsify-pdf-converter": "^2.0.7"
711
}
812
}

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

+88-1
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,102 @@
6161
<!-- 这里可写当前语言的特殊实现逻辑 -->
6262

6363
```python
64-
64+
# Definition for a binary tree node.
65+
# class TreeNode:
66+
# def __init__(self, val=0, left=None, right=None):
67+
# self.val = val
68+
# self.left = left
69+
# self.right = right
70+
class Solution:
71+
def flatten(self, root: TreeNode) -> None:
72+
"""
73+
Do not return anything, modify root in-place instead.
74+
"""
75+
while root:
76+
if root.left:
77+
pre = root.left
78+
while pre.right:
79+
pre = pre.right
80+
pre.right = root.right
81+
root.right = root.left
82+
root.left = None
83+
root = root.right
6584
```
6685

6786
### **Java**
6887

6988
<!-- 这里可写当前语言的特殊实现逻辑 -->
7089

7190
```java
91+
/**
92+
* Definition for a binary tree node.
93+
* public class TreeNode {
94+
* int val;
95+
* TreeNode left;
96+
* TreeNode right;
97+
* TreeNode() {}
98+
* TreeNode(int val) { this.val = val; }
99+
* TreeNode(int val, TreeNode left, TreeNode right) {
100+
* this.val = val;
101+
* this.left = left;
102+
* this.right = right;
103+
* }
104+
* }
105+
*/
106+
class Solution {
107+
public void flatten(TreeNode root) {
108+
while (root != null) {
109+
if (root.left != null) {
110+
// 找到当前节点左子树的最右节点
111+
TreeNode pre = root.left;
112+
while (pre.right != null) {
113+
pre = pre.right;
114+
}
115+
116+
// 将左子树的最右节点指向原来的右子树
117+
pre.right = root.right;
118+
119+
// 将当前节点指向左子树
120+
root.right = root.left;
121+
root.left = null;
122+
}
123+
root = root.right;
124+
}
125+
}
126+
}
127+
```
72128

129+
### **C++**
130+
131+
```cpp
132+
/**
133+
* Definition for a binary tree node.
134+
* struct TreeNode {
135+
* int val;
136+
* TreeNode *left;
137+
* TreeNode *right;
138+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
139+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
140+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
141+
* };
142+
*/
143+
class Solution {
144+
public:
145+
void flatten(TreeNode* root) {
146+
while (root) {
147+
if (root->left) {
148+
TreeNode *pre = root->left;
149+
while (pre->right) {
150+
pre = pre->right;
151+
}
152+
pre->right = root->right;
153+
root->right = root->left;
154+
root->left = nullptr;
155+
}
156+
root = root->right;
157+
}
158+
}
159+
};
73160
```
74161
75162
### **...**

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

+83-1
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,95 @@
5151
### **Python3**
5252

5353
```python
54-
54+
# Definition for a binary tree node.
55+
# class TreeNode:
56+
# def __init__(self, val=0, left=None, right=None):
57+
# self.val = val
58+
# self.left = left
59+
# self.right = right
60+
class Solution:
61+
def flatten(self, root: TreeNode) -> None:
62+
"""
63+
Do not return anything, modify root in-place instead.
64+
"""
65+
while root:
66+
if root.left:
67+
pre = root.left
68+
while pre.right:
69+
pre = pre.right
70+
pre.right = root.right
71+
root.right = root.left
72+
root.left = None
73+
root = root.right
5574
```
5675

5776
### **Java**
5877

5978
```java
79+
/**
80+
* Definition for a binary tree node.
81+
* public class TreeNode {
82+
* int val;
83+
* TreeNode left;
84+
* TreeNode right;
85+
* TreeNode() {}
86+
* TreeNode(int val) { this.val = val; }
87+
* TreeNode(int val, TreeNode left, TreeNode right) {
88+
* this.val = val;
89+
* this.left = left;
90+
* this.right = right;
91+
* }
92+
* }
93+
*/
94+
class Solution {
95+
public void flatten(TreeNode root) {
96+
while (root != null) {
97+
if (root.left != null) {
98+
TreeNode pre = root.left;
99+
while (pre.right != null) {
100+
pre = pre.right;
101+
}
102+
pre.right = root.right;
103+
root.right = root.left;
104+
root.left = null;
105+
}
106+
root = root.right;
107+
}
108+
}
109+
}
110+
```
60111

112+
### **C++**
113+
114+
```cpp
115+
/**
116+
* Definition for a binary tree node.
117+
* struct TreeNode {
118+
* int val;
119+
* TreeNode *left;
120+
* TreeNode *right;
121+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
122+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
123+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
124+
* };
125+
*/
126+
class Solution {
127+
public:
128+
void flatten(TreeNode* root) {
129+
while (root) {
130+
if (root->left) {
131+
TreeNode *pre = root->left;
132+
while (pre->right) {
133+
pre = pre->right;
134+
}
135+
pre->right = root->right;
136+
root->right = root->left;
137+
root->left = nullptr;
138+
}
139+
root = root->right;
140+
}
141+
}
142+
};
61143
```
62144
63145
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
112
class Solution {
213
public:
314
void flatten(TreeNode* root) {
4-
TreeNode* cur = root;
5-
while (cur) {
6-
if (cur->left) {
7-
TreeNode* p = cur->left;
8-
while (p->right) p = p->right;
9-
p->right = cur->right;
10-
cur->right = cur->left;
11-
cur->left = nullptr;
15+
while (root) {
16+
if (root->left) {
17+
TreeNode *pre = root->left;
18+
while (pre->right) {
19+
pre = pre->right;
20+
}
21+
pre->right = root->right;
22+
root->right = root->left;
23+
root->left = nullptr;
1224
}
13-
cur = cur->right;
25+
root = root->right;
1426
}
1527
}
16-
};
28+
};
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
116
class Solution {
217
public void flatten(TreeNode root) {
3-
if (root==null) return;
4-
TreeNode right = root.right;
5-
flatten(right);
6-
flatten(root.left);
7-
root.right = root.left;
8-
root.left = null;
9-
TreeNode cache = root;
10-
while (cache.right!=null) cache = cache.right;
11-
cache.right = right;
18+
while (root != null) {
19+
if (root.left != null) {
20+
TreeNode pre = root.left;
21+
while (pre.right != null) {
22+
pre = pre.right;
23+
}
24+
pre.right = root.right;
25+
root.right = root.left;
26+
root.left = null;
27+
}
28+
root = root.right;
29+
}
1230
}
1331
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def flatten(self, root: TreeNode) -> None:
9+
"""
10+
Do not return anything, modify root in-place instead.
11+
"""
12+
while root:
13+
if root.left:
14+
pre = root.left
15+
while pre.right:
16+
pre = pre.right
17+
pre.right = root.right
18+
root.right = root.left
19+
root.left = None
20+
root = root.right

summary.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
- 题解速览
2-
- [LeetCode(持续补充中...)](/solution/README.md)
3-
- [LeetCode 《剑指 Offer(第 2 版)》](/lcof/README.md)
4-
- [LeetCode 《程序员面试金典(第 6 版)》](/lcci/README.md)
2+
- [LeetCode(持续补充中...)](./solution/README.md)
3+
- [LeetCode 《剑指 Offer(第 2 版)》](./lcof/README.md)
4+
- [LeetCode 《程序员面试金典(第 6 版)》](./lcci/README.md)

summary_en.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
- All Solutions
2-
- [LeetCode(Not finished yet)](/solution/README_EN.md)
3-
- [LCOF: _Coding Interviews, 2nd Edition_](/lcof/README_EN.md)
4-
- [LCCI: _Cracking the Coding Interview, 6th Edition_](/lcci/README_EN.md)
2+
- [LeetCode(Not finished yet)](./solution/README_EN.md)
3+
- [LCOF: _Coding Interviews, 2nd Edition_](./lcof/README_EN.md)
4+
- [LCCI: _Cracking the Coding Interview, 6th Edition_](./lcci/README_EN.md)

0 commit comments

Comments
 (0)