添加 0654.最大二叉树.md C语言版本
This commit is contained in:
parent
f3b140591b
commit
6d01869bbc
|
|
@ -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);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue