O(n) Accepted Solution.


  • 0
    S
        public int count=1, val=-1;        
        public int kthSmallest(TreeNode root, int k) {
            helper(root,k);
            return val;
        }
        
        void helper(TreeNode root, int k){
            if(root==null || count>k)
                return;
            helper(root.left,k);
            if(k==count++) {
                val=root.val;
                return;
            }
            helper(root.right,k);
        }

Log in to reply
 

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