Skip to content
  • Dashboard
  • Review
  • Go: plan & chat
  • Roadmap
  • Patterns & lessons49
  • Sheets
  • Problems1.5k
  • Ascend
  • Toolkit
  • Core CS + SQL
  • System Design
  • Machine Coding
  • Interview hub
  • Mock interview
  • Companies & OA
  • Leaderboard
  • Battles
  • College Cup
  • People

Keep your progress

Sync across devices with a free account.

Sign upSign in
Patterns & lessons
Sign inSign up
Patterns/Trees/Tree DFS (Paths & Recursion)

Technique

Heavy-Light Decomposition

Split the tree into heavy chains so any path crosses O(log n) chains, then answer path queries with a segment tree over chain order.

Learn firstSegment Tree / Fenwick (BIT)Euler Tour / Tree FlatteningTree DFS (Paths & Recursion)
0%
mastery
0/2 solved
Practise nextTourists
IdeaPracticePitfalls

Recognise it

The signal

path sum/max queries with point or range updates

  1. 1Brute force

    Walk the path node by node.

  2. 2The observation

    Linear path walks: each light edge at least halves the subtree size.

  3. 3Why it works

    Positions of each heavy chain are contiguous in the base array.

Reach for it when

  • +subtree and path queries together

Not when

  • ×no updates (binary lifting with min/max tables suffices)
  • ×tree changes over time

Practice

Next

Tourists

0/2 solved
  • TouristsCodeforces3200
  • Path Queries IICSESHard

Pitfalls

Misconceptions

  • !edge values must be stored on the child node
Go deeper1 combinations · resources

Combinations

  • Heavy-Light Decomposition+Segment Tree / Fenwick (BIT)

    max/sum on path u-v with point updates

    Heavy-Light Decomposition:
    maps each path to O(log n) contiguous ranges
    Segment Tree / Fenwick (BIT):
    range max/sum per chain

    Path Queries II

Resources

  • Binary Tree explore cardLeetCode · course
  • Tree cheatsheetTech Interview Handbook · article
  • BST visualizerVisuAlgo · tool
  • Introduction to Tree AlgorithmsUSACO Guide · course
  • Lowest Common Ancestorcp-algorithms · article
  • NeetCode RoadmapNeetCode · sheet
TodayReviewPatternsProblems