Question: MODIFY THE struct TNode* sortedArrayToBST(int arr[], int start, int end) function to correctly keep track of the number of nodes in each subtree. Its current
MODIFY THE "struct TNode* sortedArrayToBST(int arr[], int start, int end)" function to correctly keep track of the number of nodes in each subtree. Its current implementation does not keep track of the members of the left and right subtree.
/* A Binary Tree node */
struct TNode
{
int data;
struct TNode* left;
struct TNode* right;
int leftSubtreeCount;
int rightSubtreeCount;
};
struct TNode* newNode(int data);
/* A function that constructs Balanced Binary Search Tree from a sorted array */
struct TNode* sortedArrayToBST(int arr[], int start, int end)
{
/* Base Case */
if (start > end)
return NULL;
/* Get the middle element and make it root */
int mid = (start + end)/2;
struct TNode *root = newNode(arr[mid]);
/* Recursively construct the left subtree and make it
left child of root */
root->left = sortedArrayToBST(arr, start, mid-1);
/* Recursively construct the right subtree and make it
right child of root */
root->right = sortedArrayToBST(arr, mid+1, end);
return root;
}
/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
struct TNode* newNode(int data)
{
struct TNode* node = (struct TNode*)
malloc(sizeof(struct TNode));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
