Browse curriculum

Return Values in DFS

Return Values in DFS: understanding how to bubble up information from the bottom of the tree.

Top-Down vs Bottom-Up

There are two main ways to solve Tree problems:

  1. Top-Down (Pre-order): Pass values down to children. (e.g., "Current path sum is 10, child add your value to 10").
  2. Bottom-Up (Post-order): Ask children for information and aggregate it. (e.g., "Left child, what is your height? Right child, what is yours?").

Interactive Visualization

h = HEIGHT A NODE RETURNED

CALL

Stack depth

1

best

:

Height

:

call stack

bottomUp(3)

 

height(3): it cannot know its height until both children answer

Next

Ask 3's left child for its height

Free preview: the first 9 of 27 steps

Tree

Compute one tree's height both ways. Bottom-up, a node's number appears when it returns, so the numbers fill in from the leaves. Top-down, a node's number is the depth it was handed, so they fill in from the root, and the answer is collected in best.

Bubbling Up Values

Most "Find height", "Find diameter", or "Check balance" problems use Bottom-Up.

Logic flow:

  1. Go down to null (Base case returns 0 or expected null value).
  2. As functions return, they pass data back to their parent.
  3. The parent calculates its own result based on left/right return values and returns that to its own parent.

Example: Logic for Height

Height = max(LeftHeight, RightHeight) + 1

This single line captures the essence of Bottom-Up DFS. You cannot know your height until your children tell you theirs.

Both Directions

The height of a tree computed bottom-up from return values, and top-down by passing depth down, in every language.

Return Values in DFS Code
1function bottomUp(node) {
2 if (!node) return 0;
3 const left = bottomUp(node.left);
4 const right = bottomUp(node.right);
5 return Math.max(left, right) + 1;
6}
7
8function topDown(root) {
9 let best = 0;
10
11 function dfs(node, depth) {
12 if (!node) return;
13 best = Math.max(best, depth);
14 dfs(node.left, depth + 1);
15 dfs(node.right, depth + 1);
16 }
17
18 dfs(root, 1);
19 return best;
20}

Return Values in DFS in Python, Java, C++, TypeScript and C

Return Values in DFS in Python
class Solution:
    def bottomUp(self, node):
        if not node:
            return 0
        left = self.bottomUp(node.left)
        right = self.bottomUp(node.right)
        return max(left, right) + 1

    def topDown(self, root):
        self.best = 0

        def dfs(node, depth):
            if not node:
                return
            self.best = max(self.best, depth)
            dfs(node.left, depth + 1)
            dfs(node.right, depth + 1)

        dfs(root, 1)
        return self.best
Return Values in DFS in Java
class Solution {
    private int best;

    public int bottomUp(TreeNode node) {
        if (node == null) return 0;
        int left = bottomUp(node.left);
        int right = bottomUp(node.right);
        return Math.max(left, right) + 1;
    }

    public int topDown(TreeNode root) {
        best = 0;
        dfs(root, 1);
        return best;
    }

    private void dfs(TreeNode node, int depth) {
        if (node == null) return;
        best = Math.max(best, depth);
        dfs(node.left, depth + 1);
        dfs(node.right, depth + 1);
    }
}
Return Values in DFS in C++
class Solution {
    int best;

public:
    int bottomUp(TreeNode* node) {
        if (!node) return 0;
        int left = bottomUp(node->left);
        int right = bottomUp(node->right);
        return max(left, right) + 1;
    }

    int topDown(TreeNode* root) {
        best = 0;
        dfs(root, 1);
        return best;
    }

    void dfs(TreeNode* node, int depth) {
        if (!node) return;
        best = max(best, depth);
        dfs(node->left, depth + 1);
        dfs(node->right, depth + 1);
    }
};
Return Values in DFS in TypeScript
function bottomUp(node: TreeNode | null): number {
    if (!node) return 0;
    const left = bottomUp(node.left);
    const right = bottomUp(node.right);
    return Math.max(left, right) + 1;
}

function topDown(root: TreeNode | null): number {
    let best = 0;

    function dfs(node: TreeNode | null, depth: number): void {
        if (!node) return;
        best = Math.max(best, depth);
        dfs(node.left, depth + 1);
        dfs(node.right, depth + 1);
    }

    dfs(root, 1);
    return best;
}
Return Values in DFS in C
static int best;

int bottomUp(struct TreeNode* node) {
    if (node == NULL) return 0;
    int left = bottomUp(node->left);
    int right = bottomUp(node->right);
    return (left > right ? left : right) + 1;
}

static void dfs(struct TreeNode* node, int depth) {
    if (node == NULL) return;
    if (depth > best) best = depth;
    dfs(node->left, depth + 1);
    dfs(node->right, depth + 1);
}

int topDown(struct TreeNode* root) {
    best = 0;
    dfs(root, 1);
    return best;
}

More practice: DSA interview questions with answers · patterns cheat sheet

The approach, step by step

  1. Bottom-up: ask the children

    Return 0 at an empty node; otherwise get the left and right heights.

  2. Bottom-up: combine

    Return max(left, right) + 1 to the parent.

  3. Top-down: pass depth down

    Record max(best, depth), call both children with depth + 1, and return best at the end.

Frequently asked questions

What is bottom-up DFS?

Each call first gets answers from its children and then combines them into its own answer, which it returns to its parent. Height is the classic example: max(left, right) + 1.


What is top-down DFS?

Each call receives information from its parent as an argument (such as the current depth) and passes an updated value to its children. The final answer is usually collected in a variable outside the calls.


When should I use each direction?

Bottom-up fits answers that depend on whole subtrees, like height, diameter or subtree sums. Top-down fits answers that depend on the path from the root, like a running sum or the largest value seen so far.

New to the technique behind this problem? Watch it run step by step:

Depth First Search Visualization

Applying for software engineering roles? Free, no sign-up:

AI resume checker: match your resume to the job description