Graph Theory37 sections · 1633 units
Open in Course

Tree DFS Template

The Parent Pointer

You pass p (parent) in the recursion to check where you came from.

function dfs(node, p):
    for child in adj[node]:
        if child != p:
            dfs(child, node)

Call it with dfs(root, 1-1) to start. The 1-1 means "no parent" for the root. Every other node passes its own index as the parent when recursing to children.

This pattern is the foundation for most tree algorithms. Memorize it.