My simple c++ solution


  • 0
    K
    class Solution {
    public:
    int counter=0;
    void helper(TreeNode * root, int k, int * result)
    {
        if(!root)return;
        if(root->left)helper(root->left, k, result);
        counter++;
        if(counter==k){
            *result=root->val;
            return ;
        }
        if(root->right)helper(root->right, k, result);
        
    }
    int kthSmallest(TreeNode* root, int k) {
        int result=0;
        helper(root, k, &result);
        return result;
    }
    

    };


Log in to reply
 

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