My simple java solution


  • 0
    L
    enter code here 
    public class Solution {
    public int kthSmallest(TreeNode root, int k) {
        if(root==null) return -1;
        List<Integer> list=new ArrayList<Integer>();
        get_node(root,list);
        return list.get(k-1);
    }
    private void get_node(TreeNode node, List<Integer> list) {
        if(node.left!=null)
            get_node(node.left,list);
        list.add(node.val);
        if(node.right!=null)
            get_node(node.right,list);
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.