Skip to main content

Lowest Common Ancestor

KANISHKA GUPTA
EditReport

Lowest Common Ancestor (LCA) in a Binary Tree

The Lowest Common Ancestor (LCA) of two nodes p and q in a binary tree is defined as the lowest node in the tree that has both p and q as descendants (where a node can be a descendant of itself).


Problem Statement

Given a binary tree and two nodes p and q, find their lowest common ancestor.

Video Explanation

Node Class Representation

Solutions

struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
Track Your Progress

Done with this topic? Mark it as complete to track your progress.

💬 Discuss this page

Have a question or spot something confusing in "Lowest Common Ancestor"? Ask below — it's backed by GitHub Discussions, so maintainers get notified like any other GitHub activity.