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

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.

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

treelcaqueriesbinary-lifting

Interactive visualization

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

node 0

parent: none

depth: 0

node 1

parent: 0

depth: 1

node 2

parent: 0

depth: 1

node 3

parent: 1

depth: 2

node 4

parent: 1

depth: 2

node 5

parent: 2

depth: 2

node 6

parent: 5

depth: 3

Path from 3 to root

310

Path from 6 to root

6520

First shared ancestor

The lowest common ancestor of 3 and 6 is 0: the deepest node that lies on both root paths.

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

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

Given two nodes in a rooted tree, find the deepest node that is an ancestor of both.

That node is their lowest common ancestor (LCA).

This query appears everywhere:

  • distance between two tree nodes
  • path queries
  • subtree reasoning
  • comparing ancestry relationships

Why it matters

LCA is one of the big “tree queries” that turns a plain rooted tree into a query-processing structure.

It also acts as a meeting point for several other topics:

  • DFS provides depth and parent information
  • Binary lifting gives one common O(logn)O(\log n) solution
  • Euler tour + sparse table gives another classic route

So LCA is not isolated. It sits exactly where the tree-query toolkit comes together.

Core idea

A node is a common ancestor if both query nodes lie in its subtree chain to the root.

The lowest common ancestor is simply the deepest such node.

A slow solution walks both nodes upward until they meet. A fast solution preprocesses the tree.

Binary lifting route

  1. preprocess depth and parent jumps
  2. lift the deeper node until both nodes have the same depth
  3. lift both nodes together from large jumps to small jumps
  4. the parent where they first agree is the LCA

This gives:

  • preprocessing: O(nlogn)O(n \log n)
  • query: O(logn)O(\log n)

Euler tour + RMQ route

Another classic method:

  1. run an Euler tour of the tree
  2. record depths along the tour
  3. the LCA of two nodes becomes a range minimum query over first occurrences

This is why LCA often appears right after Euler tour and sparse table material.

Complexity

MethodPreprocessQuery
Naive parent climbingO(1)O(1)O(n)O(n)
Binary liftingO(nlogn)O(n \log n)O(logn)O(\log n)
Euler tour + RMQO(nlogn)O(n \log n)O(1)O(1) or O(logn)O(\log n) depending on RMQ structure

Key takeaways

  • LCA is a core rooted-tree query, not a niche trick
  • It ties together depth, parent pointers, DFS order, and range-query ideas
  • Binary lifting is usually the most practical general solution
  • Euler tour + RMQ is the more reduction-style view of the same problem

Practice problems

ProblemDifficultyKey idea
Lowest Common Ancestor of a Binary Tree🟡 MediumBasic recursive LCA reasoning
Company Queries II🔴 HardFast LCA queries in rooted trees
Distance Queries🔴 HardLCA plus depth arithmetic

Relation to other topics

  • Binary lifting is the most common fast LCA implementation in this atlas
  • Euler tour technique can reduce LCA to a range minimum query
  • Sparse table is often the RMQ structure used after Euler tour preprocessing

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.