Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Kth Smallest Element in a BST problem solution

YASH PAL, 31 July 2024

In this Leetcode Kth Smallest Element in a BST problem solution you are given the root of a binary search tree, and an integer k, return the kth (1-indexed) smallest element in the tree.

Leetcode Kth Smallest Element in a BST problem solution

Problem solution in Python.

class Solution:
    def kthSmallest(self, root: TreeNode, k: int) -> int:
        def inorder(node, arr):
            if node is None:
                return arr
            arr = inorder(node.left, arr)
            arr.append(node.val)
            arr = inorder(node.right, arr)
            return arr
        
        arr = inorder(root, [])
        return arr[k-1]

Problem solution in Java.

class Solution {
    int count;
    int res;
    
    public int kthSmallest(TreeNode root, int k) {
        count = k;
        res = 0;
        helper(root);
        return res;
    }
    
    private void helper(TreeNode root) {
        if (root == null) return;
        helper(root.left);
        count--;
        if (count == 0) res = root.val;
        helper(root.right);
    }
}

Problem solution in C++.

class Solution {
public:
    int kthSmallest(TreeNode* root, int k) {
        // traverse in-order until k-th item is reached
        std::stack<TreeNode*> st;
        while (root || !st.empty())
        {
            while (root)
            {
                st.push(root);
                root = root->left;
            }
            root = st.top();
            if (--k == 0)
                return root->val;
            st.pop();
            root = root->right;
        }
        return -1;
    }
};

Problem solution in C.

int count;
int inorder(struct TreeNode* root);
int kthSmallest(struct TreeNode* root, int k){
    count=k;
    return inorder(root);
    //return 4;
}
int inorder(struct TreeNode* root){
    if(root==NULL){
        return 0;
    }
    int temp1=inorder(root->left);
    if(temp1!=0)
        return temp1;
    count-=1;
    if(count==0){
        return root->val;
    }
    int temp2=inorder(root->right);
    return temp2;
}

coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes