Skip to main content
Back to the DSA atlas
GraphsTrees Technique Advanced

Euler Tour Technique

Flatten a rooted tree into an array so subtree problems become range problems. This is the bridge between trees and range-query structures.

Trees is nested under Graphs , so techniques in the broader family usually still apply here.

treedfsrange-queryrare

Interactive visualization

Use the controls to connect the idea to concrete operations before diving into the full write-up.

DFS order flattened into an array

tour 0

A

tour 1

B

tour 2

D

tour 3

E

tour 4

C

tour 5

F

tour 6

G

Node A

tin = 0, tout = 6

children: B, C

Node B

tin = 1, tout = 3

children: D, E

Node C

tin = 4, tout = 6

children: F, G

Node D

tin = 2, tout = 2

children: none

Node E

tin = 3, tout = 3

children: none

Node F

tin = 5, tout = 5

children: none

Node G

tin = 6, tout = 6

children: none

Subtree of B

DFS entry times make the subtree of B become one contiguous interval: [1, 3]. That is why tree problems can be turned into array range queries.

Family

Graphs → Trees

Hierarchies and constrained graphs. A tree is a connected acyclic graph.

Builds on

2 topics

Read these first if you want the surrounding context.

Unlocks

3 next topics

Use these follow-ups when you want to apply the current idea in a richer setting.

Learning paths

1

This topic appears in curated progressions so you can keep moving without guessing the next step.

Problem

Tree problems often ask for operations on an entire subtree:

  • sum values in a subtree
  • add something to every node in a subtree
  • answer queries about descendants

Trees are awkward for direct range machinery, but arrays are not. The Euler tour technique converts the tree into an array while preserving subtree contiguity.

Core idea

Run a DFS and record when each node is first visited.

If tin[u] is the entry time of node u, then every node in the subtree of u appears in one contiguous segment of the DFS order.

That means:

subtree(u)[tin[u],tout[u]]\text{subtree}(u) \leftrightarrow [tin[u], tout[u]]

Now subtree queries become ordinary array range queries.

Simple flattening

One common version stores each node once, at entry time:

dfs(u, p):
    tin[u] <- timer
    order[timer] <- u
    timer <- timer + 1

    for v in children(u):
        if v != p:
            dfs(v, u)

    tout[u] <- timer - 1

With this representation, the subtree of u is exactly the interval [tin[u], tout[u]].

Why it matters

Euler tour is not the final answer by itself. It is a reduction.

Once the tree is flattened, you can use:

  • Fenwick tree for subtree sums or adds
  • segment tree for richer subtree range operations
  • prefix sums for static subtree aggregates

This is why the technique shows up everywhere in competitive programming and query-heavy tree problems.

Common variants

Entry-only tour

Store each node once. Best for subtree intervals.

Full Euler tour

Store a node each time you enter or leave it. Useful for LCA reductions and traversal traces.

Subtree size form

If you compute subtree sizes, then:

subtree(u)=[tin[u],tin[u]+size[u]1]\text{subtree}(u) = [tin[u], tin[u] + size[u] - 1]

Same idea, different bookkeeping.

Complexity

OperationTimeSpace
DFS flatteningO(n)O(n)O(n)O(n)

After flattening, the complexity depends on whatever range structure you place on top.

Key takeaways

  • Euler tour is the bridge between trees and arrays
  • It works because DFS visits every subtree in one contiguous block
  • By itself it does not answer queries; it makes trees compatible with Fenwick trees, segment trees, and prefix sums
  • It is rare in beginner material, but extremely important once tree queries appear

Practice problems

ProblemDifficultyKey idea
Subtree Queries🟡 MediumFlatten the tree, then use a Fenwick tree
Company Queries I🟡 MediumRooted tree preprocessing, often paired with binary lifting
Path Queries🔴 HardEuler tour plus a range-query structure

Relation to other topics

  • DFS provides the traversal order that makes the flattening possible
  • Fenwick tree and segment tree usually sit on top of the flattened array
  • Binary lifting solves ancestor jumps; Euler tour solves subtree interval mapping

Build on these first

These topics supply the mental model or underlying structure that this page assumes.

What this enables

Once the current idea feels natural, these are the most useful next jumps.

Related directions

These topics live nearby conceptually, even if they are not direct prerequisites.

More from Trees

Stay in the same family when you want parallel variations of the same mental model.

Paths that include this topic

Follow one of these sequences if you want a guided next step instead of open-ended browsing.

From the blog

Pair the atlas with recent writing from the rest of the site when you want a broader engineering perspective alongside the topic graph.