# Lowest Common Ancestor of Two Nodes in a Binary Tree | Aced

Aced (formerly Exponent) publishes a public search API over its indexed pages; the guide is at https://www.aced.io/agents/agent-access.

Source: https://www.aced.io/questions/1156/lowest-common-ancestor-binary-tree
Source observed: 2026-11-04T23:00:02.583Z
Last checked: 2026-11-04T23:00:02.583+00:00
Indexed: 2026-11-06T03:28:14.464+00:00

---

# Find the lowest common ancestor (LCA) of two nodes in a binary tree

**Difficulty:** Medium  
**Category:** Data Structures & Algorithms  
**Also categorized as:** Coding

We provide this coding interview question for practicing binary-tree algorithms. You can [sign up to run code](https://www.aced.io/signup?src=lesson), save the question, or use the available Question, Solution, Community, Editor, Share, Output, Test Results, and Console views. Detailed company statistics are available through [Upgrade](https://www.aced.io/upgrade?src=questions).

## Problem

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.

For nodes `p` and `q`, the lowest common ancestor is the lowest node in the tree that has both `p` and `q` as descendants. A node is allowed to be a descendant of itself.

## Example

Consider this binary tree:

```text
        10
       /  \
      5    15
     / \     \
    3   7     20
       /     /
      1     8
             \
              17
```

- The LCA of nodes `5` and `15` is node `10`.
- The LCA of nodes `3` and `7` is node `5`.

## Interview metadata

The question includes interview metadata and was reported as last asked **6 months ago** at the observation time of **2026-11-04**. The rendered page lists these role filters:

- [Software Engineer](https://www.aced.io/questions?role=swe) — 4
- [Data Scientist](https://www.aced.io/questions?role=data-science)
- [Machine Learning Engineer](https://www.aced.io/questions?role=ml-engineer)
- [Data Engineer](https://www.aced.io/questions?role=data-engineer)

It lists these company filters:

- [Amazon](https://www.aced.io/questions?company=amazon) — 2
- [Meta](https://www.aced.io/questions?company=meta-facebook)
- [Apple](https://www.aced.io/questions?company=apple)
- [LinkedIn](https://www.aced.io/questions?company=linkedin)
- [Spotify](https://www.aced.io/questions?company=spotify)
- [Oracle](https://www.aced.io/questions?company=oracle)

## Community

No answers have been contributed by the community in the supplied content. The rendered interface labels the Community tab with **9**.

## Interview experiences

Two shared Amazon interview experiences are associated with this question:

1. [Amazon Software Engineer Intern interview](https://www.aced.io/experiences/amazon-software-engineer-intern-interview-19b58a)
   - **Timing:** 6 months ago
   - **Role:** Software Engineer · Intern
   - **Sample participant comment:** “It went well and answered all questions. and did both leetcode questions asked one in each interview”

2. [Amazon Software Engineer interview](https://www.aced.io/experiences/amazon-software-engineer-interview-9e3c13)
   - **Timing:** a year ago
   - **Role:** Software Engineer · Entry Level / L3
   - **Sample participant comment:** “Very long process and dragged on for almost a month, more annoying considering the fact that I was still in the early stages of the process”

## Related questions

- [Find the first common ancestor of two nodes in a binary tree.](https://www.aced.io/questions/4538/binary-tree-first-common-ancestor)
- [Find the common ancestors in a tree.](https://www.aced.io/questions/3443/find-common-ancestors-tree)
- [Print the top view of nodes in a binary tree.](https://www.aced.io/questions/5570/top-view-nodes-binary-tree)

## Related courses

### [Machine Learning Engineer Interview Prep](https://www.aced.io/courses/ml-engineer)

![Machine Learning Engineer Interview Prep](https://images.ctfassets.net/x78yjrjc11pq/6UfkHxUyLQVfDHSfLNQtWa/1be1539a65b4f6f15201f5b573c0a578/ML_Engineer_Interview_Prep_Course.jpeg?w=800&h=800&fm=webp&q=75)

We help candidates prepare for machine learning roles at Meta, Google, Amazon, Apple, Microsoft, Nvidia, and other companies through mock interviews, frameworks, and advice from senior candidates. Topics include ML system design, core concepts, coding, and behavioral interviews.

### [Data Science Interview Prep](https://www.aced.io/courses/data-science)

![Data Science Interview Prep](https://images.ctfassets.net/x78yjrjc11pq/3U4C5C0OkDR0UeGch4AmlE/7ad489430b676a088cb973307fe77884/Data_Science_Interview_Prep_Course.jpeg?w=800&h=800&fm=webp&q=75)

We help candidates prepare for data-science roles at Google, Amazon, Microsoft, Meta, Apple, and other companies through mock interviews, frameworks, and advice from senior candidates. Topics include statistics, experimentation, coding, SQL, machine learning, and behavioral interviews.

## Aced interview-preparation areas

Our navigation on this page links to interview-preparation resources including:

- [Product Management](https://www.aced.io/courses/pm?src=nav) — strategy cases and technical skills
- [Forward Deployed](https://www.aced.io/courses/forward-deployed-engineering?src=nav) — FDE loops at Anthropic, OpenAI, Palantir, and more
- [Engineering Management](https://www.aced.io/courses/engineering-management?src=nav) — leadership and people-management skills
- [Software Engineering](https://www.aced.io/courses/software-engineering?src=nav) — coding-problem strategies
- [System Design](https://www.aced.io/courses/system-design-interviews?src=nav) — architectures, interfaces, and databases
- [Data Science](https://www.aced.io/courses/data-science?src=nav) — statistical techniques and experimentation
- [Machine Learning](https://www.aced.io/courses/ml-engineer?src=nav) — building, evaluating, and deploying AI/ML models
- [Data Engineering](https://www.aced.io/courses/data-engineering?src=nav) — data models and ETL pipelines
- [AI Engineering](https://www.aced.io/courses/ai-engineering?src=nav)
- [Investment Banking](https://www.aced.io/ib?src=nav)
- [Generative AI](https://www.aced.io/courses/gen-ai-interviews?src=nav)
- [Security Engineer](https://www.aced.io/courses/security-engineer?src=nav)
- [TPM](https://www.aced.io/courses/tpm?src=nav)
- [Solutions Architect](https://www.aced.io/courses/solution-architect-interview?src=nav)
- [UX / Product Design](https://www.aced.io/courses/product-designer-interview?src=nav)
- [BizOps & Strategy](https://www.aced.io/courses/bizops?src=nav)

We also link to [Loops](https://www.aced.io/guides?src=nav), [Experiences](https://www.aced.io/experiences?src=nav), [all questions](https://www.aced.io/questions?src=nav), [Practice](https://www.aced.io/practice?src=nav), [Coaching](https://www.aced.io/coaching?src=nav), [Pricing](https://www.aced.io/upgrade?src=nav), [Job Referrals](https://www.aced.io/jobs/referrals?src=nav), our [Blog](https://www.aced.io/blog?src=nav), [Resources](https://www.aced.io/resources?src=nav), [Work with us](https://www.aced.io/collaborate?src=nav), [Perks](https://www.aced.io/perks?src=nav), [university services](https://www.aced.io/pages/university?src=nav), [business services](https://www.aced.io/pages/business?src=nav), [recruiter job posting](https://www.aced.io/jobs/post?src=nav), and our [Affiliate program](https://www.aced.io/affiliate?src=nav).

## Access

- [Sign up](https://www.aced.io/signup?src=nav)
- [Log in](https://www.aced.io/login?src=nav)
- [Upgrade](https://www.aced.io/upgrade?src=questions)

---

[Read this page’s summary](https://www.aced.io/agents/api/public/v1/page-tldr?url=https%3A%2F%2Fwww.aced.io%2Fquestions%2F1156%2Flowest-common-ancestor-binary-tree&trace=t_679ff9ab98f3)

## Ask a follow-up about this page

Need a more specific answer? Ask the exact question you have. It returns a fresh, cited answer from Aced (formerly Exponent)’s knowledge base. No account or API key required.

Open this URL with your own question, URL-encoded. Include your question in q. All research inputs are optional; no account or setup.

`GET https://www.aced.io/agents/agent/faq?q={question}&trace=t_679ff9ab98f3`

[Tool guide and examples](https://www.aced.io/agents/agent-access) · [OpenAPI](https://www.aced.io/agents/api/public/v1/openapi.json) · [Page TLDR form](https://www.aced.io/agents/agent-tools/page-tldr?url=https%3A%2F%2Fwww.aced.io%2Fquestions%2F1156%2Flowest-common-ancestor-binary-tree). Opening a form does not execute a tool.