DSA Guide
Trees

Trees

Depth-first and breadth-first traversal, and choosing between recursion and an explicit stack

A tree is a graph with no cycles and exactly one path between any two nodes. That guarantee is what makes tree algorithms simple: you never need a "visited" set, because you can never arrive somewhere twice.

Two Ways to Walk a Tree

Depth-first search (DFS) — a stack

Follow one branch to its end before backtracking. Natural to express recursively, since the call stack does the bookkeeping.

The three orders differ only in when the node itself is recorded:

        1
       / \
      2   3
     / \
    4   5

Preorder  (node, left, right):  1, 2, 4, 5, 3
Inorder   (left, node, right):  4, 2, 5, 1, 3
Postorder (left, right, node):  4, 5, 2, 3, 1

Inorder on a binary search tree gives sorted output

That single fact solves a surprising number of BST problems: validation, finding the k-th smallest, and converting to a sorted list all reduce to "do an inorder walk and check something".

Breadth-first search (BFS) — a queue

Visit every node at depth d before any node at depth d + 1.

Choosing Between Them

DFSBFS
Structurestack / recursionqueue
SpaceO(height)O(width)
Best forsubtree computations, path enumerationlevel grouping, minimum depth
Deep skewed treerisks stack overflowuses O(1) space
Wide balanced treeuses O(log n) spaceholds up to n/2 nodes

The space trade-off is real: DFS is cheap on wide trees, BFS is cheap on deep ones.

Node Definitions

from typing import Optional


class TreeNode:
    def __init__(
        self,
        val: int = 0,
        left: Optional["TreeNode"] = None,
        right: Optional["TreeNode"] = None,
    ):
        self.val = val
        self.left = left
        self.right = right

Recursion depth limits

Python raises RecursionError at around 1,000 frames by default; JavaScript engines throw RangeError at a similar depth. A balanced tree of a million nodes is only ~20 deep and is perfectly safe — but a degenerate chain of 10,000 nodes will crash a recursive solution. That is when the explicit-stack version earns its extra lines.

All Problems

On this page