Merge pull request #901 from KingArthur0205/remote

添加 0654.最大二叉树.md C语言版本, 0222.完全二叉树的结点个数.md C语言版本, 添加 0104.二叉树的最大深度.md C语言版本, 以及 添加 1047.删除字符串中的所有相邻重复项.md C语言版本
This commit is contained in:
程序员Carl
2021-11-17 15:53:42 +08:00
committed by GitHub
5 changed files with 210 additions and 1 deletions

View File

@@ -353,6 +353,35 @@ var constructMaximumBinaryTree = function (nums) {
};
```
## C
```c
struct TreeNode* traversal(int* nums, int left, int right) {
//若左边界大于右边界返回NULL
if(left >= right)
return NULL;
//找出数组中最大数坐标
int maxIndex = left;
int i;
for(i = left + 1; i < right; i++) {
if(nums[i] > nums[maxIndex])
maxIndex = i;
}
//开辟结点
struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct TreeNode));
//将结点的值设为最大数组数组元素
node->val = nums[maxIndex];
//递归定义左孩子结点和右孩子结点
node->left = traversal(nums, left, maxIndex);
node->right = traversal(nums, maxIndex + 1, right);
return node;
}
struct TreeNode* constructMaximumBinaryTree(int* nums, int numsSize){
return traversal(nums, 0, numsSize);
}
```
-----------------------