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

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.

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

treepath-queriessegment-treerare

Interactive visualization

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

nodeparentdepthchainbase-array pos
0-0A0
101A1
201C5
312A2
412B4
522C6
633A3
753C7

Path broken into heavy-chain segments

chain C: [5, 7]
chain A: [0, 3]

Tree paths become a few array segments

Climb chain heads until both nodes share a chain, then each piece is just a segment-tree interval.

Family

Graphs → Trees

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

Builds on

4 topics

Read these first if you want the surrounding context.

Unlocks

1 next topic

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

Learning paths

2

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

Problem

Subtree queries are pleasant after an Euler tour because a subtree becomes one interval. Path queries are harder.

Examples:

  • sum along the path from u to v
  • maximum edge on a path
  • update every node on a path

A path is not usually one contiguous subtree interval, so you need another reduction.

Core idea

For every node, mark one child as heavy: usually the child with the largest subtree. The remaining child edges are light.

Heavy edges form disjoint chains. The key fact is:

any root-to-node path crosses only O(log n) light edges

So any path between two nodes can be decomposed into only O(log n) chain segments.

Once each chain is flattened into a base array, a segment tree can answer queries on each segment.

Query sketch

The usual pattern is:

  1. while the chain heads of u and v differ
  2. move the deeper chain head upward
  3. query that whole chain segment in the base array
  4. when both nodes land on the same chain, finish with one last contiguous segment

That turns one hard tree path into a small number of range queries.

Why it matters

Heavy-light decomposition is the path-query counterpart to Euler tour.

  • Euler tour is best when you care about subtrees
  • HLD is best when you care about paths

Together, they complete the mental model for turning trees into arrays.

Complexity

OperationTime
Preprocessing decompositionO(n)O(n) or O(nlogn)O(n \log n) depending on implementation details
One path query/update with segment treeO(log2n)O(\log^2 n)

With more specialized structures, some implementations reduce constants or one logarithm, but the classic version is O(log2n)O(\log^2 n).

Key takeaways

  • HLD is the standard reduction for path queries on trees
  • The win comes from crossing only O(log n) light edges
  • Segment trees usually sit underneath the decomposition
  • This is a rare advanced topic, but it is the natural next step after LCA and Euler tour

Practice problems

ProblemDifficultyKey idea
Path Queries II🔴 HardClassic HLD plus segment tree
Query on a tree again!🔴 HardEdge/path queries with HLD
Cow Land🔴 HardTree path xor queries

Relation to other topics

  • Lowest Common Ancestor helps reason about path structure and split points
  • Euler Tour Technique solves subtree intervals, while HLD solves arbitrary path intervals
  • Segment Tree is the usual range structure placed on top of the decomposed chains
  • Persistent Segment Tree is one possible upgrade once ordinary HLD feels comfortable

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.

Tree path queries

Build the full ladder from ancestor queries to heavy-light decomposition and historical segment trees.

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.