Explanation

599.Sum of N Numbers Using Recursion

Easy
Given n, return 1 + 2 + … + n — computed with recursion rather than a loop or a formula. RecursionRecursionA function solving a problem by calling itself on a smaller version of the same problem. expresses the sum as "n plus the sum of the smaller problem", and needs a base caseBase CaseThe input for which a recursive function returns directly without calling itself, so the recursion stops. so the calls eventually stop. Every waiting call occupies a frame on the call stackCall StackThe memory area that holds one frame per active function call, in last-in, first-out order., so the shape of the recursion decides how deep that stack grows: n levels for the plain version, about log n when the range is split in half.

Example 1:

Input: n = 5

Output: 15

Example 2:

Input: n = 0

Output: 0

+ 5 hidden test cases run on Submit.

Constraints:

  • ●0 ≤ n ≤ 500

n =

5