Recursive Javascript Solution


  • 0
    G
    function combinations(n, k) {
      let result = [];
    
      function traverse(arr, depth) {
        if (arr.length === k) {
          result.push(arr);
          return;
        }
        
        if (depth > n) {      
          return;
        }
    
        traverse(arr, depth + 1);  
        traverse(arr.concat(depth), depth + 1);
      }
    
      traverse([], 1);
    
      return result;
    }
    

Log in to reply
 

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