From 43297632db14ab349081a7a6237612fe4db5a13c Mon Sep 17 00:00:00 2001 From: chayan das Date: Sat, 18 Oct 2025 23:47:46 +0530 Subject: [PATCH] Create 18 October Median of BST --- 18 October Median of BST | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 18 October Median of BST diff --git a/18 October Median of BST b/18 October Median of BST new file mode 100644 index 0000000..9271b19 --- /dev/null +++ b/18 October Median of BST @@ -0,0 +1,22 @@ +class Solution { + public: + int countNodes(Node* root) { + if (!root) return 0; + return 1 + countNodes(root->left) + countNodes(root->right); + } + + int findKth(Node* root, int& k) { + if (!root) return -1; + int left = findKth(root->left, k); + if (left != -1) return left; + k--; + if (k == 0) return root->data; + return findKth(root->right, k); + } + + int findMedian(Node* root) { + int total = countNodes(root); + int k = (total + 1) / 2; + return findKth(root, k); + } +};