Simple and Clean Java solution with explanation
public static int ans = 0;\n    public int kthSmallest(TreeNode root, int k) {\n        helper(root, k);\n        return ans;\n    }\n    \n    public int helpe
    public static int ans = 0;\n    public int kthSmallest(TreeNode root, int k) {\n        helper(root, k);\n        return ans;\n    }\n    \n    public int helper(TreeNode root, int k) {\n        if (root == null) {\n            return 0;\n        }\n        int leftCount = helper(root.left, k);\n        int rightCount = helper(root.right, k - leftCount - 1);\n        if (k == leftCount + 1) {\n            ans = root.val;\n        }\n        return leftCount + rightCount + 1;\n    }\n\nWe count the number of nodes of left sub tree and right sub tree recursively. Suppose the Kth smallest element is in the right sub tree, then we need to update k as k - leftCount - 1 (leftCount + 1 is the number of nodes of left sub tree plus the root node). Only when k equals leftCount + 1, we find the target.
['Java']