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

Binary Lifting & LCA

Precompute up[k][v] = 2k-th ancestor (or successor) so k-th ancestor, LCA, and path aggregates take O(log n).

Learn firstTree DFS (Paths & Recursion)Bit Tricks & BitmaskingUnlocksKruskal Reconstruction TreeDominator TreeBlock-Cut Tree / Bridge Tree
0%
mastery
0/19 solved
Practise nextQpwoeirut and Vertices
IdeaPracticePitfalls

Recognise it

The signal

many LCA / distance / k-th ancestor queries

  1. 1Brute force

    Walk parent pointers one by one: O(depth) per query.

  2. 2The observation

    Linear walks: any jump length is a sum of powers of two.

  3. 3Why it works

    up[k][v] = up[k-1][up[k-1][v]].

Reach for it when

  • +successor after k steps in a functional graph
  • +path min/max on a static tree

Not when

  • ×path updates (hld + seg-tree)
  • ×tree changes (link-cut trees)

Practice

Next

Qpwoeirut and Vertices

0/19 solved
  • New Roads QueriesCSESMedium
  • Cyclic ArrayCSESMedium
  • Distance QueriesCSESMedium
  • Company Queries IICSESMedium
  • Company Queries ICSESMedium
  • Planets Queries ICSESMedium
  • Counting PathsCSESMedium
  • Qpwoeirut and VerticesCodeforces2300
  • Kingdom and its CitiesCodeforces2800
  • Count PathsCodeforces2000
  • Forbidden CitiesCSESHard
  • Movie Festival QueriesCSESHard

Pitfalls

Misconceptions

  • !forgetting to lift the deeper node to equal depth first
Go deepervariants and techniques · 7 combinations · resources

Variants and techniques

  • Virtual (Auxiliary) TreeCP 2400+For a query that marks k vertices: sort them by DFS entry time, add the LCAs of adjacent pairs, and build an O(k)-node tree that keeps the ancestor relations. Running the per-query tree DP on it costs O(sum k log n) in total.

Combinations

  • Binary Lifting & LCA+Tree DFS (Paths & Recursion)

    distance between two nodes, k-th ancestor, many queries

    Binary Lifting & LCA:
    jumps to ancestors in O(log n)
    Tree DFS (Paths & Recursion):
    computes depth and parent

    Distance QueriesCompany Queries IICompany Queries I

  • Functional / Successor Graphs+Binary Lifting & LCA

    teleporters/successor after k steps; first time reaching b from a

    Functional / Successor Graphs:
    every node has one successor
    Binary Lifting & LCA:
    k-step successor in O(log k)

    Planets Queries IPlanets Queries II

  • Virtual (Auxiliary) Tree+DP on Trees+Binary Lifting & LCA

    many queries with a small vertex set each and sum of k bounded

    Virtual (Auxiliary) Tree:
    shrink the tree to the O(k) marked vertices and their LCAs
    DP on Trees:
    run the separation/cover DP on the small tree
    Binary Lifting & LCA:
    LCA queries while building the virtual tree

    Kingdom and its CitiesCount Paths

  • Kruskal Reconstruction Tree+Binary Lifting & LCA

    the minimum possible maximum edge between u and v (or the first time they connect) for many pairs

    Kruskal Reconstruction Tree:
    turns each union into a node valued by the edge weight
    Binary Lifting & LCA:
    LCA queries, or jumping to the highest ancestor with value <= x

    Qpwoeirut and VerticesNew Roads Queries

  • Binary Lifting & LCA+Interval Scheduling (sort by end)

    many queries: how many non-overlapping intervals fit inside [a, b]

    Binary Lifting & LCA:
    jump 2^k greedy steps at once
    Interval Scheduling (sort by end):
    from time t, the greedy next movie is the one ending earliest among those starting >= t

    Movie Festival Queries

  • Binary Lifting & LCA+Monotonic Stack

    count elements of the 'next greater' chain starting at a that lie within b

    Binary Lifting & LCA:
    count how many next-greater jumps stay within [a, b]
    Monotonic Stack:
    next greater element for every index

    Visible Buildings Queries

  • Block-Cut Tree / Bridge Tree+Binary Lifting & LCA

    queries: can a reach b without visiting c (undirected)

    Block-Cut Tree / Bridge Tree:
    turns 'must pass through c' into a tree-path membership test
    Binary Lifting & LCA:
    LCA / ancestor checks for the path test

    Forbidden Cities

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