Straight forward Java inorder traverse solution


  • 0
    M

    '''

    class Solution {

    int counter = 0;
    
    int result = 0;
    
    public int kthSmallest(TreeNode root, int k) {
    
        if (root == null) return Integer.MIN_VALUE;
    
        kthSmallest(root.left, k);
        counter++;
        if (counter == k) result = root.val;
        kthSmallest(root.right, k);
        return result;
    }
    

    }

    '''

    and I found it really does not matter what value is assigned to result at the very beginning if there guarantee an anser.


Log in to reply
 

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