Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void fillValue(vector<int>& num,int l,int r, TreeNode* &node){
if(l>r) return;
int m=(l+r)/2;
node=new TreeNode(num[m]);
fillValue(num,l,m-1,node->left);
fillValue(num,m+1,r,node->right);
}
TreeNode *sortedArrayToBST(vector<int> &num) {
TreeNode* head=NULL;
fillValue(num,0,num.size()-1,head);
return head;
}
};