1ms Java Recursive Solution


  • 0
    O
    int smallestKthVal;
        
    public int kthSmallest(TreeNode root, int k) {
        findSmallestKth(root, k, 0);
        return smallestKthVal
    } 
    
    private int findSmallestKth(TreeNode root, int k, int i) {
        if (root == null) return i;
         i = findSmallestKth(root.left, k, i);
         i++;
         if (i == k) {
             smallestKthVal = root.val;
             return i;
         } else {
             return findSmallestKth(root.right, k, i);
         }
     }
    

Log in to reply
 

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