Java 1ms recursive solution


  • 2
    G

    Java straight forward recursive solution.

    import java.util.*;
    public class Solution {
        private int count=0;
        private int result = -1;
        public int kthSmallest(TreeNode root, int k) {
            if(count<k && root.left != null){
                kthSmallest(root.left, k);
            }
            if(++count == k){
                result = root.val;
            }
            if(count<k && root.right != null){
                kthSmallest(root.right, k);
            }
            return result;
        }
    }

Log in to reply
 

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