1. class Solution {
    2. List<Integer> list = new ArrayList<>();
    3. public void dfs(TreeNode root){
    4. if(root == null) return;
    5. dfs(root.left);
    6. list.add(root.val);
    7. dfs(root.right);
    8. }
    9. public int kthSmallest(TreeNode root, int k){
    10. list = new ArrayList<>();
    11. dfs(root);
    12. for(int i = 0; i < list.size(); i++){
    13. if(i == k - 1)
    14. return list.get(i);
    15. }
    16. return -1;
    17. }
    18. }