Skip to content

Commit

Permalink
Merge pull request youngyangyang04#875 from KingArthur0205/remote
Browse files Browse the repository at this point in the history
添加 0108.将有序数组转换为二叉搜索树.md 0538.把二叉搜索树转换为累加树.md C语言版本
  • Loading branch information
youngyangyang04 committed Oct 29, 2021
2 parents 2a585bb + c9845b1 commit ff2fcae
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
19 changes: 18 additions & 1 deletion problems/0108.将有序数组转换为二叉搜索树.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,25 @@ var sortedArrayToBST = function (nums) {
};
```

## C
递归
```c
struct TreeNode* traversal(int* nums, int left, int right) {
if (left > right)
return NULL;
int mid = left + ((right - left) / 2);
struct TreeNode* root = (struct TreeNode*)malloc(sizeof(struct TreeNode));
root->val = nums[mid];
root->left = traversal(nums, left, mid - 1);
root->right = traversal(nums, mid + 1, right);
return root;
}


struct TreeNode* sortedArrayToBST(int* nums, int numsSize) {
struct TreeNode* root = traversal(nums, 0, numsSize - 1);
return root;
}
```
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
Expand Down
21 changes: 21 additions & 0 deletions problems/0538.把二叉搜索树转换为累加树.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,27 @@ var convertBST = function (root) {
};
```

##C

递归
```c
int pre;
void traversal(struct TreeNode* node) {
if(!node)
return ;
traversal(node->right);
node->val = node->val + pre;
pre = node->val;
traversal(node->left);
}

struct TreeNode* convertBST(struct TreeNode* root){
pre = 0;
traversal(root);
return root;
}
```
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
Expand Down

0 comments on commit ff2fcae

Please sign in to comment.