2023每日刷题(五十八)
Leetcode—108.将有序数组转换为二叉搜索树
实现代码
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
struct TreeNode* dfs(int *arr, 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 = arr[mid];
root->left = dfs(arr, left, mid - 1);
root->right = dfs(arr, mid + 1, right);
return root;
}
struct TreeNode* sortedArrayToBST(int* nums, int numsSize) {
return dfs(nums, 0, numsSize - 1);
}
运行结果
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!