@@ -78,6 +78,33 @@ class Solution:
78
78
return head
79
79
```
80
80
81
+ ``` python
82
+ # Definition for a binary tree node.
83
+ # class TreeNode:
84
+ # def __init__(self, val=0, left=None, right=None):
85
+ # self.val = val
86
+ # self.left = left
87
+ # self.right = right
88
+ class Solution :
89
+ def increasingBST (self , root : TreeNode) -> TreeNode:
90
+ def dfs (root : TreeNode):
91
+ if root is None :
92
+ return
93
+
94
+ dfs(root.left)
95
+
96
+ nonlocal cur
97
+ cur.right = root
98
+ root.left = None
99
+ cur = cur.right
100
+
101
+ dfs(root.right)
102
+
103
+ cur = dummy = TreeNode()
104
+ dfs(root)
105
+ return dummy.right
106
+ ```
107
+
81
108
### ** Java**
82
109
83
110
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -123,6 +150,45 @@ class Solution {
123
150
}
124
151
```
125
152
153
+ ``` java
154
+ /**
155
+ * Definition for a binary tree node.
156
+ * public class TreeNode {
157
+ * int val;
158
+ * TreeNode left;
159
+ * TreeNode right;
160
+ * TreeNode() {}
161
+ * TreeNode(int val) { this.val = val; }
162
+ * TreeNode(int val, TreeNode left, TreeNode right) {
163
+ * this.val = val;
164
+ * this.left = left;
165
+ * this.right = right;
166
+ * }
167
+ * }
168
+ */
169
+ class Solution {
170
+ private TreeNode cur;
171
+
172
+ public TreeNode increasingBST (TreeNode root ) {
173
+ TreeNode dummy = new TreeNode ();
174
+ cur = dummy;
175
+ dfs(root);
176
+ return dummy. right;
177
+ }
178
+
179
+ private void dfs (TreeNode root ) {
180
+ if (root == null ) {
181
+ return ;
182
+ }
183
+ dfs(root. left);
184
+ cur. right = root;
185
+ root. left = null ;
186
+ cur = cur. right;
187
+ dfs(root. right);
188
+ }
189
+ }
190
+ ```
191
+
126
192
### ** Go**
127
193
128
194
``` go
@@ -158,6 +224,34 @@ func increasingBST(root *TreeNode) *TreeNode {
158
224
}
159
225
```
160
226
227
+ ``` go
228
+ /* *
229
+ * Definition for a binary tree node.
230
+ * type TreeNode struct {
231
+ * Val int
232
+ * Left *TreeNode
233
+ * Right *TreeNode
234
+ * }
235
+ */
236
+ func increasingBST (root *TreeNode ) *TreeNode {
237
+ dummy := &TreeNode{}
238
+ cur := dummy
239
+ var dfs func (*TreeNode)
240
+ dfs = func (root *TreeNode) {
241
+ if root == nil {
242
+ return
243
+ }
244
+ dfs (root.Left )
245
+ root.Left = nil
246
+ cur.Right = root
247
+ cur = root
248
+ dfs (root.Right )
249
+ }
250
+ dfs (root)
251
+ return dummy.Right
252
+ }
253
+ ```
254
+
161
255
### ** C++**
162
256
163
257
``` cpp
@@ -199,6 +293,39 @@ public:
199
293
};
200
294
```
201
295
296
+ ```cpp
297
+ /**
298
+ * Definition for a binary tree node.
299
+ * struct TreeNode {
300
+ * int val;
301
+ * TreeNode *left;
302
+ * TreeNode *right;
303
+ * TreeNode() : val(0), left(nullptr), right(nullptr) {}
304
+ * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
305
+ * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
306
+ * };
307
+ */
308
+ class Solution {
309
+ public:
310
+ TreeNode* increasingBST(TreeNode* root) {
311
+ TreeNode* dummy = new TreeNode();
312
+ TreeNode* cur = dummy;
313
+ function<void(TreeNode*)> dfs = [&](TreeNode* root) {
314
+ if (!root) {
315
+ return;
316
+ }
317
+ dfs(root->left);
318
+ cur->right = root;
319
+ root->left = nullptr;
320
+ cur = cur->right;
321
+ dfs(root->right);
322
+ };
323
+ dfs(root);
324
+ return dummy->right;
325
+ }
326
+ };
327
+ ```
328
+
202
329
### ** TypeScript**
203
330
204
331
``` ts
0 commit comments