Browse curriculum

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

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}

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.

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

AI resume checker — match your resume to the job description