Practice Discrete Math

Algorithms / Euler Tour Subtree Ranges

Least You Need to Know: Euler Tours, Subtree Intervals, and Flattened Trees

An Euler-tour-style entry order can flatten a rooted tree so each subtree becomes a **contiguous interval** in an array. That converts many subtree problems into familiar range-query problems.

The least you need to know

Key notation

tin[u] entry time of node `u` in DFS order
subtree_size[u] number of nodes in the subtree rooted at `u`
[tin[u], tin[u] + size[u]) array interval covering the subtree in flattened order

Tiny worked example

  • Suppose DFS visits nodes in entry order `1, 2, 4, 5, 3, 6`.
  • If node `2` has descendants `4` and `5`, then its subtree occupies the contiguous block `2, 4, 5` in that entry list.
  • A subtree-sum query can then become a range-sum query on that block.
  • This is why Euler tours pair naturally with Fenwick and segment trees.

Common mistakes

How to recognize this kind of problem

Next recommended lesson

Continue through this topic with Least You Need to Know: Fast/Slow Pointers, Middle Nodes, and Cycle Entry.

Least You Need to Know: Fast/Slow Pointers, Middle Nodes, and Cycle Entry

Related lessons

Keep going with nearby lessons in the same topic.

More ways to explore

Start practice