```
public class Solution {
public int kthSmallest(TreeNode root, int k) {
int leftNum = countNodes(root.left);
if (leftNum + 1 == k)
return root.val;
else if (leftNum + 1 > k) {
return kthSmallest(root.left, k);
}
else
return kthSmallest(root.right, k - leftNum - 1);
}
private int countNodes(TreeNode root) {
if (root == null)
return 0;
return 1 + countNodes(root.left) + countNodes(root.right);
}
}
```

The idea is to determine where the target falls (left side, the root, or the right side) at each level.