Data Structures19 sections · 729 units
Open in Course

Euler Tour - Construction

The implementation

Here's how to build the Euler tour:

function eulerTour(root)
    timer := 0
    dfs(root, -1)

function dfs(v, parent)
    tin[v] := timer
    timer := timer + 1
    tour[tin[v]] := v

    for each child u of v (u  parent)
        dfs(u, v)

    tout[v] := timer - 1

The tour array stores nodes in DFS order. For subtree of vv, indices [tin[v],tout[v]][tin[v], tout[v]] contain exactly the nodes in vv's subtree.