Hi, as for second method, DFS traverse.

I don't think the solution is optimal, for that code, if dfs return void, the time complexity is always o(n). we would have to run through all nodes.

My method intend to do early return, to avoid "run all nodes". Below is my code, please feel free to point any bugs, thanks

class Solution {
int counter;
int target;
public int kthSmallest(TreeNode root, int k) {
counter = 1;
inorder(root, k);
return target;
}
//whether we could find kth(globally) in "root" rooted subtree
private boolean inorder(TreeNode root, int k) {
if (root == null) {
return false;
}
if (inorder(root.left, k)) {
return true;
}
if (counter == k) {
target = root.val;
return true;
}
counter++;
return inorder(root.right, k);
}
}