what this question want to say about bst…and which hint it indicates …and what hint it given please explain asap
What this question want to say about bst.....and which hint it indicates
In the question… You are given the preorder traversal of the tree… So Preorder traversal of the tree is by default a sorted array… so … You need to convert an array of sorted integers to BST… So you can follow the approach as ,
node* buildHeightBalancedTree(int *arr, int start, int end)
{
if (start > end) {
return NULL;
}
int mid = start + (end - start) / 2;
node* root = new node(arr[mid]);
root->left = buildHeightBalancedTree(arr, start, mid - 1);
root->right = buildHeightBalancedTree(arr, mid + 1, end);
return root;