C++ solution

  • 0
    int kthSmallest(TreeNode* root, int k) {
        return helper(root,k)->val;
    TreeNode* helper(TreeNode * root,int& k){
        if(root==NULL) return NULL;
        TreeNode *l=helper(root->left,k);
        if(l) return l;
        if(k>1) k--;
        else return root;//if(cnt>=k-1)
        TreeNode *r=helper(root->right,k);
        if(r) return r;
        return NULL;

Log in to reply

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