✅ Beats 100% t 97.63%s|| simple explanation || all solutions
Intuition\nBrute Force:\nput all elements in the tree node and perform sorting \n\nNOTE: In a BST inorder(L-N-R) traversal will always be in sorterd order\n\nno
# Intuition\nBrute Force:\nput all elements in the tree node and perform sorting \n```\nNOTE: In a BST inorder(L-N-R) traversal will always be in sorterd order\n```\nnow from above observation \n\nkth element is the kth smnallest element.(optmized)\n\nkth largest element = N-k or you can also try R-N-L (traverses indescending order)\n\n# Complexity\n```\n\nSolution  \tTime \tSpace \n-------------------------\nRecursive\tO(N)\tO(H)\nIterative\tO(N)\tO(H)\n\n```\n\n# Code\n```java []\n//Recursion_DFS\nclass Solution {\n    public int kthSmallest(TreeNode root, int k) {\n        int[] result = new int[2]; // 0: kth smallest value, 1: current count\n        \n        inorderTraversal(root, k, result);\n        \n        return result[0];\n    }\n    \n    private void inorderTraversal(TreeNode node, int k, int[] result) {\n        if (node == null || result[1] >= k) {\n            return;\n        }\n        inorderTraversal(node.left, k, result);\n    \n        result[1]++;  // Increment the count\n        \n        // Check if the current element is the kth smallest\n        if (result[1] == k) {\n            result[0] = node.val;\n            return;\n        }\n        \n        inorderTraversal(node.right, k, result);\n    }\n}\n```\n```Java []\n//Iterative DFS_Inorder\nclass Solution {\n    public int kthSmallest(TreeNode root, int k) {\n    Stack<TreeNode> stack = new Stack<>();\n    \n    while (root != null || !stack.isEmpty()) {\n        while (root != null) {\n            stack.push(root);\n            root = root.left;\n        }\n        root = stack.pop();\n        // Decrease k since we\'ve visited one element\n        k--;\n\n        if (k == 0) {\n            break; // Break the loop as we\'ve found the kth smallest element\n        }\n        root = root.right;\n    }\n    \n    return root.val; // Return the value of the kth smallest element\n    }\n}\n```\n\n![image.png](https://assets.leetcode.com/users/images/212fd08b-fc27-4151-95e9-63d5fa85a681_1692814819.0278614.png)\n\n**Kth Largest**\n```java []\n\npublic int kthLargest(TreeNode root, int k) {\n        int N = countNodes(root); // Get the total number of nodes in the tree\n        int[] result = new int[2]; // 0: kth largest value, 1: current count\n\n        k = N - k + 1; // Convert to kth largest\n\n        inorderTraversal(root, k, result);\n\n        return result[0];\n    }\n\n    private int countNodes(TreeNode node) {\n        if (node == null) {\n            return 0;\n        }\n        return 1 + countNodes(node.left) + countNodes(node.right);\n    }\n\n    private void inorderTraversal(TreeNode node, int k, int[] result) {\n        if (node == null || result[1] >= k) {\n            return;\n        }\n        inorderTraversal(node.left, k, result);\n\n        result[1]++; // Increment the count\n\n        // Check if the current element is the kth largest\n        if (result[1] == k) {\n            result[0] = node.val;\n            return;\n        }\n\n        inorderTraversal(node.right, k, result);\n    }\n\n```\n```Java []\n//traversing R-N-L \nclass Solution {\n    public int kthLargest(TreeNode root, int k) {\n        Stack<TreeNode> stack = new Stack<>();\n        TreeNode current = root;\n        \n        while (current != null || !stack.isEmpty()) {\n            while (current != null) {\n                stack.push(current);\n                current = current.right; // Traverse right subtree\n            }\n            \n            current = stack.pop();\n            k--;\n            \n            if (k==0) {\n                return current.val;\n            }\n            \n            current = current.left; // Traverse left subtree\n        }\n        \n        return root.val; // If k is out of bounds\n    }\n}\n\n```\n```Java []\n//using R-N-L traversing in descending order\nclass Solution {\n        public int kthLargest(TreeNode root, int k) {\n        int[] result = new int[2]; // 0: kth smallest value, 1: current count\n\n        inorderTraversal(root, k, result);\n\n        return result[0];\n    }\n\n    private void inorderTraversal(TreeNode node, int k, int[] result) {\n        if (node == null || result[1] >= k) {\n            return;\n        }\n        inorderTraversal(node.right, k, result);\n\n        result[1]++;  // Increment the count\n\n        // Check if the current element is the kth smallest\n        if (result[1] == k) {\n            result[0] = node.val;\n            return;\n        }\n\n        inorderTraversal(node.left, k, result);\n    }\n}\n\n```\n\n<img src="https://assets.leetcode.com/users/images/787ae0f1-23ee-4bd0-b6d5-657ac428b838_1693806287.9120827.gif" alt="closed_paren" title="Please Upvote" style="display: block; margin: 0 auto;" width="120" height="200"/>\n<strong style="display: block; text-align: center; font-weight: bold;">This is a tree series solution, please upvote if you find this helpful.</strong>\n
['Java']