18. Median of BST
β GFG solution to the Median of BST problem: find median element in a BST using space-efficient Morris Traversal technique without recursion or stack. π
π§© Problem Description
π Examples
Example 1
Input: root = [20, 8, 22, 4, 12, N, N, N, N, 10, 14]
Output: 12
Explanation: The inorder of given BST is 4, 8, 10, 12, 14, 20, 22.
Here, n = 7 (odd), so median will be ((7+1)/2)th value = 4th value = 12.Example 2
Input: root = [5, 4, 8, 1]
Output: 4
Explanation: The inorder of given BST is 1, 4, 5, 8.
Here, n = 4 (even), so median will be (4/2)th value = 2nd value = 4.π Constraints
β
My Approach
Morris Traversal Technique
π Time and Auxiliary Space Complexity
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
πVisitor Count
Last updated