@@ -199,6 +199,124 @@ public:
199
199
};
200
200
```
201
201
202
+ ### **TypeScript**
203
+
204
+ ```ts
205
+ /**
206
+ * Definition for a binary tree node.
207
+ * class TreeNode {
208
+ * val: number
209
+ * left: TreeNode | null
210
+ * right: TreeNode | null
211
+ * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
212
+ * this.val = (val===undefined ? 0 : val)
213
+ * this.left = (left===undefined ? null : left)
214
+ * this.right = (right===undefined ? null : right)
215
+ * }
216
+ * }
217
+ */
218
+
219
+ function increasingBST(root: TreeNode | null): TreeNode | null {
220
+ const dummy = new TreeNode();
221
+ let cur = dummy;
222
+ const dfs = (root: TreeNode | null) => {
223
+ if (root == null) {
224
+ return;
225
+ }
226
+ dfs(root.left);
227
+ cur.right = new TreeNode(root.val);
228
+ cur = cur.right;
229
+ dfs(root.right);
230
+ };
231
+ dfs(root);
232
+ return dummy.right;
233
+ }
234
+ ```
235
+
236
+ ### ** Rust**
237
+
238
+ ``` rust
239
+ // Definition for a binary tree node.
240
+ // #[derive(Debug, PartialEq, Eq)]
241
+ // pub struct TreeNode {
242
+ // pub val: i32,
243
+ // pub left: Option<Rc<RefCell<TreeNode>>>,
244
+ // pub right: Option<Rc<RefCell<TreeNode>>>,
245
+ // }
246
+ //
247
+ // impl TreeNode {
248
+ // #[inline]
249
+ // pub fn new(val: i32) -> Self {
250
+ // TreeNode {
251
+ // val,
252
+ // left: None,
253
+ // right: None
254
+ // }
255
+ // }
256
+ // }
257
+ use std :: rc :: Rc ;
258
+ use std :: cell :: RefCell ;
259
+ impl Solution {
260
+ fn dfs (root : & Option <Rc <RefCell <TreeNode >>>, vals : & mut Vec <i32 >) {
261
+ if root . is_none () {
262
+ return ;
263
+ }
264
+ let node = root . as_ref (). unwrap (). borrow ();
265
+ Self :: dfs (& node . left, vals );
266
+ vals . push (node . val);
267
+ Self :: dfs (& node . right, vals );
268
+ }
269
+
270
+ pub fn increasing_bst (root : Option <Rc <RefCell <TreeNode >>>) -> Option <Rc <RefCell <TreeNode >>> {
271
+ let mut vals = Vec :: new ();
272
+ Self :: dfs (& root , & mut vals );
273
+ let mut dummy = Rc :: new (RefCell :: new (TreeNode :: new (0 )));
274
+ for & val in vals . iter (). rev () {
275
+ let mut dummy = dummy . as_ref (). borrow_mut ();
276
+ dummy . right = Some (Rc :: new (RefCell :: new (TreeNode {
277
+ val ,
278
+ left : None ,
279
+ right : dummy . right. take (),
280
+ })));
281
+ }
282
+ let ans = dummy . as_ref (). borrow_mut (). right. take ();
283
+ ans
284
+ }
285
+ }
286
+ ```
287
+
288
+ ### ** C**
289
+
290
+ ``` c
291
+ /* *
292
+ * Definition for a binary tree node.
293
+ * struct TreeNode {
294
+ * int val;
295
+ * struct TreeNode *left;
296
+ * struct TreeNode *right;
297
+ * };
298
+ */
299
+
300
+ struct TreeNode *dfs (struct TreeNode * root, struct TreeNode * cur) {
301
+ if (!root) {
302
+ return cur;
303
+ }
304
+ cur = dfs(root->left, cur);
305
+ cur->right = malloc(sizeof(struct TreeNode));
306
+ cur->right->val = root->val;
307
+ cur->right->left = NULL;
308
+ cur->right->right = NULL;
309
+ cur = cur->right;
310
+ return dfs(root->right, cur);
311
+ }
312
+
313
+ struct TreeNode * increasingBST(struct TreeNode * root) {
314
+ struct TreeNode * dummy = malloc(sizeof(struct TreeNode));
315
+ dfs(root, dummy);
316
+ return dummy->right;
317
+ }
318
+ ```
319
+
202
320
### **...**
203
321
204
322
```
0 commit comments