My recursive cpp code


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

Log in to reply
 

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