A recursive way of C++ solution


  • 0
    L
    `int kthSmallest(TreeNode* root, int k)
    

    {

       int  count = countTree(root->left) + 1;
       if(count == k) return root->val;
       else if(count > k)
              return kthSmallest(root->left, k);
       else
              return kthSmallest(root->right, k-count);
    

    }
    private:

    int countTree(TreeNode* root)  
    {  
         if(root == NULL) return 0;
         int cl = 0, cr = 0;
         if(root->left) cl = countTree(root->left);
         if(root->right) cr = countTree(root->right);
         return cl+cr+1;
    }
    

    }; `


Log in to reply
 

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