代码实现:
/*** Definition for a binary tree node.* struct TreeNode {* int val;* struct TreeNode *left;* struct TreeNode *right;* };*/ struct TreeNode* buildTree(int *nums, int l, int r) {if (l > r) {return NULL; // 递归出口}struct TreeNode *root = (struct TreeNode*)malloc(sizeof(struct TreeNode));int mid = (r + l) / 2;root->val = nums[mid];root->left = buildTree(nums, l, mid - 1);root->right = buildTree(nums, mid + 1, r);return root; }struct TreeNode* sortedArrayToBST(int *nums, int numsSize){if (nums == NULL || numsSize < 1) {return NULL;}// 二分构建树int l = 0, r = numsSize - 1;return buildTree(nums, l, r); }