1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 TreeNode *sortedArrayToBST(vector<int> &num) { 13 // Start typing your C/C++ solution below 14 // DO NOT write int main() function 15 TreeNode *t; 16 int len=num.size(); 17 int mid=len/2; 18 if(num.size()==0) 19 return NULL; 20 if(num.size()==1) 21 { 22 t=(TreeNode *)malloc(sizeof(struct TreeNode)); 23 t->val=num[0]; 24 t->left=t->right=NULL; 25 return t; 26 } 27 t=(TreeNode *)malloc(sizeof(struct TreeNode)); 28 t->val=num[mid]; 29 vector<int>v1(num.begin(),num.begin()+mid); 30 vector<int>v2(num.begin()+mid+1,num.end()); 31 t->left=sortedArrayToBST(v1); 32 t->right=sortedArrayToBST(v2); 33 return t; 34 } 35 };