X DAYS

:

HOUR

:

MINS

:

SEC

Copied to Clipboard
Median of BST
Easy Accuracy: 43.23% Submissions: 29996 Points: 2

Given a Binary Search Tree of size N, find the Median of its Node values.

Example 1:

Input:
6
/   \
3      8
/  \    /  \
1    4  7    9
Output: 6
Explanation: Inorder of Given BST will be:
1, 3, 4, 6, 7, 8, 9. So, here median will 6.


Example 2:

Input:
6
/   \
3      8
/   \    /
1    4   7
Output: 5
Explanation:Inorder of Given BST will be:
1, 3, 4, 6, 7, 8. So, here median will
(4 + 6)/2 = 10/2 = 5.

You don't need to read input or print anything. Your task is to complete the function findMedian() which takes the root of the Binary Search Tree as input and returns the Median of Node values in the given BST.
Median of the BST is:

• If number of nodes are even: then median = (N/2 th node + (N/2)+1 th node)/2
• If number of nodes are odd : then median = (N+1)/2th node.

Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Height of the Tree).

Constraints:
1<=N<=1000

### Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

#### My Submissions:  