Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    DSADSA

    Foundations: measuring code

    • Big-O: how code grows
    • Space and the time–memory trade-off
    • Arrays and strings under the hood
    • Hash maps and sets
    • How to approach any interview problem

    Array patterns

    • Two pointers
    • Sliding window
    • Prefix sums
    • Binary search, done right
    • Sorting: merge sort and quicksort

    Stacks, queues and linked lists

    • Stacks and monotonic stacks
    • Queues and deques
    • Linked lists

    Recursion and trees

    • Recursion without fear
    • Backtracking: try and undo
    • Binary trees
    • Binary search trees
    • Heaps and priority queues

    Graphs and dynamic programming

    • Graphs: BFS and DFS
    • Shortest paths, topological sort and union-find
    • Dynamic programming: the idea
    • DP on grids and strings
    • Greedy: the best choice right now
    DSA

    Recursion and trees · Lesson 14 of 23

    Recursion without fear

    The base case, the call stack, and how to trust a function that calls itself.

    13 minutes2 games

    A recursive function solves a problem by calling itself on a smaller version of it. Every recursive function needs two things:

    1. A base case: a problem small enough to answer directly. 2. A shrinking step: every call moves closer to the base case.

    Forget the base case and it calls itself forever, until "Maximum call stack size exceeded".

    JavaScript
    function factorial(n) {
      if (n <= 1) return 1              // base case
      return n * factorial(n - 1)       // a smaller version of the same problem
    }
    
    // Sum of digits: 4096 → 4 + 0 + 9 + 6
    function digitSum(n) {
      if (n < 10) return n
      return (n % 10) + digitSum(Math.floor(n / 10))
    }
    
    console.log(factorial(5), digitSum(4096))
    Step by step1 / 6
    1. 1function f(n) {
    2. 2 if (n <= 1) return 1
    3. 3 return n * f(n - 1)
    4. 4}
    5. ▶console.log(f(3))

    Variables

    stackf(3)

    Output

    …

    f(3) needs f(2).

    Each call waits for the one below it to finish: that's the call stack.

    Trust: "if it works on the smaller one…"

    Don't try to trace every call in your head. Think like proof by induction: assume the function returns the right answer for the smaller problem, and only ask: "how do I use that answer to solve this one?". That's exactly how you'll handle trees.

    JavaScript
    // Recursion that repeats work: fib(40) makes over 300 million calls
    // Memoization stores each answer the first time, so every n is computed once
    function fib(n, memo = new Map()) {
      if (n <= 1) return n
      if (memo.has(n)) return memo.get(n)
      const value = fib(n - 1, memo) + fib(n - 2, memo)
      memo.set(n, value)
      return value
    }
    
    console.log(fib(10), fib(50))

    Important

    Every recursive call takes a stack slot, so depth = memory. JavaScript allows roughly 10,000–20,000 levels. If the depth can get large (a long linked list, a lopsided tree), convert to a loop with a stack you manage yourself.

    What's the output?

    function count(n) {
      if (n === 0) return ""
      return count(n - 1) + n
    }
    console.log(count(4))

    What will this code print?

    Interview question

    What's the difference between recursion and iteration, and when do you pick each?

    Exercise

    Write a fast recursive power(x, n): x^n = (x^(n/2))², with one more multiplication by x when n is odd (n ≥ 0). It must print 1024 1 243.

    Expected output: 1024 1 243

    JavaScript
    function power(x, n) {
      
    }
    
    console.log(power(2, 10), power(7, 0), power(3, 5))

    Practise on real problems

    Practice problems that use what you learned here. Solve them in JavaScript, Python or C++.

    • Climbing stairsEasy
    • Fast powerMedium
    • Pascal's triangleEasy

    Learn more from

    • Wikipedia: Recursion (computer science)en.wikipedia.org
    • Khan Academy: Recursive algorithmskhanacademy.org
    • VisuAlgo: Recursion treevisualgo.net

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousLinked listsNextBacktracking: try and undo
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.