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.
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:
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:
Same idea, different bookkeeping.
Complexity
| Operation | Time | Space |
|---|---|---|
| DFS flattening |
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
| Problem | Difficulty | Key idea |
|---|---|---|
| Subtree Queries | 🟡 Medium | Flatten the tree, then use a Fenwick tree |
| Company Queries I | 🟡 Medium | Rooted tree preprocessing, often paired with binary lifting |
| Path Queries | 🔴 Hard | Euler 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.
DFS — Depth-First Search
Dive as deep as possible before backtracking. DFS is essential for detecting cycles, topological sorting, and exploring all paths in a graph.
Tree Fundamentals
Understand roots, parents, children, depth, height, and traversal orders. A tree is a graph with enough structure to make many problems much simpler.
What this enables
Once the current idea feels natural, these are the most useful next jumps.
Binary Lifting
Jump upward in a rooted tree by powers of two to answer k-th ancestor and many LCA-style queries quickly.
Heavy-Light Decomposition
Break tree paths into O(log n) heavy-chain segments so path queries can ride on top of a segment tree instead of walking edge by edge.
Lowest Common Ancestor
Find the deepest shared ancestor of two nodes in a rooted tree. LCA is a core tree-query problem that ties together DFS, binary lifting, and Euler tours.
Related directions
These topics live nearby conceptually, even if they are not direct prerequisites.
Binary Lifting
Jump upward in a rooted tree by powers of two to answer k-th ancestor and many LCA-style queries quickly.
Heavy-Light Decomposition
Break tree paths into O(log n) heavy-chain segments so path queries can ride on top of a segment tree instead of walking edge by edge.
Lowest Common Ancestor
Find the deepest shared ancestor of two nodes in a rooted tree. LCA is a core tree-query problem that ties together DFS, binary lifting, and Euler tours.
Fenwick Tree (Binary Indexed Tree)
Maintain prefix sums with O(log n) updates and queries using one tiny bit trick. Fenwick trees are the lightest dynamic range-query structure worth knowing.
More from Trees
Stay in the same family when you want parallel variations of the same mental model.
Tree Fundamentals
Understand roots, parents, children, depth, height, and traversal orders. A tree is a graph with enough structure to make many problems much simpler.
Binary Search Tree (BST)
A sorted tree structure enabling O(log n) search, insert, and delete. Foundation for balanced trees, sets, and maps.
Heap & Priority Queue
A complete binary tree where every parent is smaller (or larger) than its children. The backbone of Dijkstra, event scheduling, and top-k problems.
Binary Lifting
Jump upward in a rooted tree by powers of two to answer k-th ancestor and many LCA-style queries quickly.
Paths that include this topic
Follow one of these sequences if you want a guided next step instead of open-ended browsing.
Rooted tree queries
Flatten subtrees, jump through ancestors, and break tree paths into array segments instead of climbing naively.
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.