/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
class Solution {
public:
/**
*
* @param num int整型vector
* @return TreeNode类
*/
TreeNode* function(vector<int>& num, int l, int r) {
if(r - l < 1)
return nullptr;
int mid = (l + r) / 2;
TreeNode* root = new TreeNode(num[mid]);
root->left = function( num, l, mid); // 生成左子树
root->right = function( num, mid + 1, r); // 生成右子树
return root;
}
TreeNode* sortedArrayToBST(vector<int>& num) {
// write code here
int n = num.size();
if(n < 1)
return nullptr;
TreeNode* root; // 生成根节点
root = function(num, 0, n);
return root;
}
};