Complexity of a Recursive Binary Tree Sum: Balanced Versus Skewed Trees

Read the full interview experience this question came from →

Quick Overview

A verbal complexity question about a recursive function that sums all node values in a binary tree. It asks for the time and space complexity and how the recursion depth differs between balanced and skewed trees, testing understanding of call-stack memory and deep recursion.

Complexity of a Recursive Binary Tree Sum: Balanced Versus Skewed Trees

Company: Duolingo

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Technical Screen

A recursive function computes the sum of all node values in a binary tree: ```python def tree_sum(node): if node is None: return 0 return node.val + tree_sum(node.left) + tree_sum(node.right) ``` What are its time and space complexity for a tree with $n$ nodes? How does the space complexity differ between a balanced tree and the worst-case tree shape? This was asked as a verbal question. ```hint Where the memory goes The function creates no data structures. Ask what is still held in memory while the deepest call is running. ``` ### Clarifying Questions - Should the space analysis include the call stack, or only memory the function allocates explicitly? - Can the tree be empty? ### What a Strong Answer Covers - Time complexity justified by counting calls, including the calls on empty children - Space complexity stated in terms of the tree height, with the reason - The balanced case and the degenerate chain case, with their bounds - Practical consequences of deep recursion, and an alternative that avoids it ### Follow-up Questions - The default recursion limit in Python is 1,000 frames. What happens on a 100,000-node chain, and how would you fix it? - Can you compute the sum with $O(1)$ extra space? What does that cost? - How do the bounds change for a tree in which each node can have any number of children?

Overview: A verbal complexity question about a recursive function that sums all node values in a binary tree. It asks for the time and space complexity and how the recursion depth differs between balanced and skewed trees, testing understanding of call-stack memory and deep recursion.

Read the full Duolingo Software Engineer interview experience this question came from

|Home/Software Engineering Fundamentals/Duolingo
Duolingo logo
Duolingo
Oct 4, 2026
easySoftware EngineerTechnical ScreenSoftware Engineering Fundamentals
1
0

A recursive function computes the sum of all node values in a binary tree:

def tree_sum(node):
    if node is None:
        return 0
    return node.val + tree_sum(node.left) + tree_sum(node.right)

What are its time and space complexity for a tree with nn nodes? How does the space complexity differ between a balanced tree and the worst-case tree shape? This was asked as a verbal question.

Clarifying Questions Guidance

  • Should the space analysis include the call stack, or only memory the function allocates explicitly?
  • Can the tree be empty?

What a Strong Answer Covers Guidance

  • Time complexity justified by counting calls, including the calls on empty children
  • Space complexity stated in terms of the tree height, with the reason
  • The balanced case and the degenerate chain case, with their bounds
  • Practical consequences of deep recursion, and an alternative that avoids it

Follow-up Questions Guidance

  • The default recursion limit in Python is 1,000 frames. What happens on a 100,000-node chain, and how would you fix it?
  • Can you compute the sum with O(1)O(1) extra space? What does that cost?
  • How do the bounds change for a tree in which each node can have any number of children?