Tree DFS: definition

Depth-first search explores one branch as far as possible before returning to explore another. Recursion uses the call stack; iterative DFS stores pending nodes in an explicit stack.

Watch the idea in action

Imagine exploring rooms connected like a tree. Follow one branch until you cannot go farther. Then walk back to the nearest room with an unexplored branch.

The key idea
This example visits a node before its children, left side first. DFS goes deep before going sideways. Returning to a parent is not a new first visit.

Problems this pattern can solve

Find tree depth, check root-to-leaf paths, compare subtrees, or combine child results to compute a diameter or check balance.

Task example: Maximum Depth of Binary Tree

Find the largest number of nodes on any path from the root to a leaf. Return zero for an empty tree. Count nodes, not edges.

Example 1
Input
root = [3,9,20,null,null,15,7]
Output
3

The paths 3 → 20 → 15 and 3 → 20 → 7 each contain three nodes.

Example 2
Input
root = []
Output
0

An empty tree has no nodes, so its depth is zero.

The editor provides TreeNode with val, left, and right. Your function receives the root node or None, not an array.

LeetCode 104 ↗
01 / Recursive DFS

Recursive DFS: video explanation

root = [3,9,20,null,null,15,7]
output: 3

Prefer reading? Follow the steps below.

  1. If the current subtree is empty, return 0. This is the base case that stops recursion.
  2. Recursively find the left depth, then the right depth. The call stack remembers the parent while each child is explored.
  3. Return 1 + max(left, right). The 1 counts the current node. A leaf therefore returns 1.

Time complexity · O(n)

Every node is visited once, so time complexity is O(n).

Space complexity · O(h)

Active calls use O(h) space, where h is the tree height. This is O(log n) for a balanced tree and O(n) for a skewed tree. Very deep trees may exceed Python's recursion limit.

PYTHONLive code trace
02 / Iterative DFS

Iterative DFS: video explanation

root = [3,9,20,null,null,15,7]
output: 3

Prefer reading? Follow the steps below.

  1. Return 0 if the root is missing. Otherwise push (root, 1) and set deepest to 0.
  2. Pop a node and its depth. Update deepest. Push its right child, then its left child, each with depth + 1. The last pushed node is explored next.
  3. When the stack is empty, return deepest. Store depths explicitly: the stack size is not the tree depth.

Both approaches have O(n) time and O(h) worst-case extra space. Iteration avoids Python's recursion limit; it is not an asymptotic time optimization.

Time complexity · O(n)

Each node is pushed and popped once, so time complexity is O(n).

Space complexity · O(h)

Pending branches need O(h) space in the worst case, at most O(n). A single chain keeps only one pending node in this implementation.

PYTHONLive code trace

Tree DFS · The task

Close or press Esc to return to your place. The task button stays pinned while you explore the solutions.