Java O(n) simple solution


  • -1
    M

    public class Solution
    {
    int count=0;
    int element=0;

    public int kthSmallest(TreeNode root, int k) {

    inorder(root, k);
    return element;
    

    }

    void inorder(TreeNode root, int k){

    if(root==null)
    return;

        inorder(root.left,k);
        count++;
         if(count==k)
        element = root.val;
        
        inorder(root.right,k);
    }
    

    }


Log in to reply
 

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