29. Median of BST
The problem can be found at the following link: Question Link
My Approach
LOL GFG made this question appear easy, but it is not that easy. Although the task itself seems straightforward – perform Inorder traversal to get a vector of elements and then find the median from that vector – the real challenge lies in achieving it with limited auxiliary space. We need to ensure the solution uses only O(h) auxiliary space (where h is the height of the tree), not O(n) (where n is the number of all nodes). So let's figure out the solution within the constraints.
To find the median of a Binary Search Tree (BST)
First, we calculate the total number of nodes in the BST using the
totalNodesfunction, which is a simple recursive function that counts the number of nodes in the left subtree, right subtree, and the current node.Next, we perform the modified inorder traversal using the
modifiedInorderfunction. This function maintains aprevpointer to keep track of the previously visited node, anivariable to keep track of the current node's position, and anoutvariable to store the median.During the inorder traversal, when we reach the
midposition (the middle element for odd-sized trees or the left-middle element for even-sized trees), we calculate the median accordingly. If the total number of nodes isodd, the median is the value of the node at the mid position. Otherwise, for even-sized trees, the median can be found by taking the average of the values stored in the nodes at positionsmidandmid-1. These positions refer to the currentrootnode and theprevnode, respectively.
Time and Auxiliary Space Complexity
Time Complexity: The time complexity of the
totalNodesfunction is O(N), whereNis the number of nodes in the BST. ThemodifiedInorderfunction takesO(N)time since we visit each node once. Therefore, the overall time complexity of thefindMedianfunction isO(N).Auxiliary Space Complexity:
O(H), whereHis the height of the BST. This is because themodifiedInorderfunction uses recursive calls, and the maximum height of the function call stack is equal to the height of the BST.
Code (C++)
int totalNodes(Node *root)
{
if(!root)
return 0;
return totalNodes(root->left) + totalNodes(root->right) + 1;
}
void modifiedInorder(Node* root, Node* &prev, int mid, int &i, bool isOddSize, float &out )
{
if(!root)
return;
modifiedInorder(root->left, prev, mid, i, isOddSize, out );
if(i == mid){
if(isOddSize)
out = root->data;
else
out = ((float)prev->data + (float)root->data)/2;
}
++i;
prev = root;
modifiedInorder(root->right, prev, mid, i, isOddSize, out);
}
float findMedian(struct Node *root)
{
int n = totalNodes(root);
Node* prev = NULL;
bool isOddSize = n%2;
int mid = (n/2) + 1;
int i = 1;
float out = 0;
modifiedInorder(root, prev, mid, i, isOddSize, out);
return out;
}Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star to the getlost01/gfg-potd repository.
Last updated
Was this helpful?