Given a Binary Search Tree of size **N**, your task is to complete the function **findMedian()**, that return the median of Binary Search Tree.

Median of the BST are:

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

For example:

```
Given BST(with odd no. of nodes) is :
6
/ \
3 8
/ \ / \
1 4 7 9
Inorder of Given BST will be : 1, 3, 4, 6, 7, 8, 9
So, here median will 6.
Given BST(with even no. of nodes) is :
6
/ \
3 8
/ \ /
1 4 7
Inorder of Given BST will be : 1, 3, 4, 6, 7, 8
So, here median will (4+6)/2 = 5.
```

**Input:**

The function takes a single argument as input, the reference pointer to the **root** of the BST.

There will be **T **test cases and for each test case function will be called separately.

**Output:**

For each test case the function should return the Median of the BST.

**Constraints:**

1<=T<=100

1<=N<=500

**Example:
Input:**

2

3

9 10 11

6

1 3 4 6 7 8

10

5

**Note:**The **Input/Ouput** format and **Example** given are used for system's internal purpose, and should be used by a user for **Expected Output** only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

preparecodinginterview_dot_com | 190 |

hari32147 | 162 |

JonSnow_IKnowNothing | 160 |

AdityaRaj9 | 112 |

shivamsingh00236 | 111 |

abducodes | 906 |

harshtyagi36 | 447 |

arpit_anshuman | 335 |

vamsikrishna55 | 323 |

sandipbhaumik | 321 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4567 |

Login to report an issue on this page.