Lowest Common Ancestor of a Binary Tree
Try to solve the Lowest Common Ancestor of a Binary Tree problem.
We'll cover the following
Statement
Given the root node of a binary tree with nodes, your task is to find the lowest common ancestor of two of its nodes, p
and q
.
Note: The lowest common ancestor of two nodes,
p
andq
, is defined as the lowest node in the binary tree that has bothp
andq
as descendants.A node can also be a descendant of itself. For example, if
q
is a descendant ofp
, and we know thatp
is a descendant of itself, thenp
will be the lowest common ancestor ofp
andq
.
Constraints:
- All
Node.data
are unique. p
q
p
andq
exist in the tree.
Examples
Create a free account to view this lesson.
Continue your learning journey with a 14-day free trial.
By signing up, you agree to Educative's Terms of Service and Privacy Policy