Untitled

                Never    
Java
       
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int kthSmallest(TreeNode root, int k) {
        
        //inorder traversal in BST is increasing order
        result res = new result();
        count c = new count();
            
        inorder(root, k, c, res);
        
        return res.res;
        
        
    }
    
    //create classes to store state between runs
    public class count {
        int c = 0;
    }
    
    public class result {
        int res = 0;
    }
    
    public void inorder(TreeNode root, int k, count c, result res) {
        
        
        if(root == null)
            return;
        
        //visit left
        inorder(root.left, k, c, res);
        
        //visit root
        c.c++; //increment current index visited
        
        if(c.c == k){ //if we are at required index
            res.res = root.val;
            System.out.println("the " + k + "th value is " + res.res);
            return;
        }
        
        //right
        inorder(root.right, k, c, res);
    }
}

Raw Text