```
public class Solution {
public int kthSmallest(TreeNode root, int k) {
Stack<TreeNode> stack = new Stack<>();
ArrayList<Integer> arr = new ArrayList<>();
TreeNode node = root;
while(!stack.empty() || node != null){
if(node != null){
stack.push(node);
node = node.left;
}
else{
node = stack.pop();
arr.add(node.val);
node = node.right;
}
}
return arr.get(k-1);
}
}
```