Skip to content

Commit

Permalink
添加 0654.最大二叉树.md C语言版本
Browse files Browse the repository at this point in the history
  • Loading branch information
KingArthur0205 committed Nov 11, 2021
1 parent f3b1405 commit 6d01869
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions problems/0654.最大二叉树.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,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);
}
```
-----------------------
Expand Down

0 comments on commit 6d01869

Please sign in to comment.