Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
Analysis:
Because the requirement "height balanced", this problem becomes relative easy.
Just recursively use the middle element in the array as the root node of the current tree(sub tree).
Read full article from Yu's Coding Garden : leetcode Question 23: Convert Sorted Array to Binary Search Tree
Analysis:
Because the requirement "height balanced", this problem becomes relative easy.
Just recursively use the middle element in the array as the root node of the current tree(sub tree).
TreeNode* cbst(vector<
int
> &num,
int
st,
int
ed){
if
(st>ed){
return
NULL;
}
else
{
int
mid = st+(ed-st)/2;
TreeNode *bst=
new
TreeNode(num[mid]);
bst->left = cbst(num,st,mid-1);
bst->right = cbst(num,mid+1,ed);
return
bst;
}
}
TreeNode *sortedArrayToBST(vector<
int
> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if
(num.size()==0){
return
NULL;}
return
cbst(num,0,num.size()-1);
}