Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
#include<iostream>
#include<vector>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
TreeNode * CreatBST(vector<int> &num, int first, int last)
{
if (first > last)
return NULL;
int mid = (first + last) / 2;
TreeNode* curnode = new TreeNode(num[mid]);
curnode->left = CreatBST(num, first, mid - 1);
curnode->right = CreatBST(num, mid + 1, last);
return curnode;
}
TreeNode *sortedArrayToBST(vector<int> &num) {
if (num.empty())
return NULL;
return CreatBST(num, 0, num.size() - 1);
}