File tree Expand file tree Collapse file tree 2 files changed +39
-1
lines changed Expand file tree Collapse file tree 2 files changed +39
-1
lines changed Original file line number Diff line number Diff line change @@ -375,8 +375,25 @@ var sortedArrayToBST = function (nums) {
375
375
};
376
376
```
377
377
378
+ ## C
379
+ 递归
380
+ ``` c
381
+ struct TreeNode* traversal (int* nums, int left, int right) {
382
+ if (left > right)
383
+ return NULL;
384
+ int mid = left + ((right - left) / 2);
385
+ struct TreeNode* root = (struct TreeNode* )malloc(sizeof(struct TreeNode));
386
+ root->val = nums[ mid] ;
387
+ root->left = traversal(nums, left, mid - 1);
388
+ root->right = traversal(nums, mid + 1, right);
389
+ return root;
390
+ }
378
391
379
-
392
+ struct TreeNode* sortedArrayToBST(int* nums, int numsSize) {
393
+ struct TreeNode* root = traversal(nums, 0, numsSize - 1);
394
+ return root;
395
+ }
396
+ ```
380
397
381
398
-----------------------
382
399
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
Original file line number Diff line number Diff line change @@ -293,6 +293,27 @@ var convertBST = function (root) {
293
293
};
294
294
```
295
295
296
+ ##C
297
+
298
+ 递归
299
+ ``` c
300
+ int pre;
301
+ void traversal (struct TreeNode* node) {
302
+ if(!node)
303
+ return ;
304
+ traversal(node->right);
305
+ node->val = node->val + pre;
306
+ pre = node->val;
307
+ traversal(node->left);
308
+ }
309
+
310
+ struct TreeNode* convertBST(struct TreeNode* root){
311
+ pre = 0;
312
+ traversal(root);
313
+ return root;
314
+ }
315
+ ```
316
+
296
317
-----------------------
297
318
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
298
319
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
You can’t perform that action at this time.
0 commit comments