Problem
You get the root of a binary tree and two different nodes p and q of that tree. The input shows the tree as a list in level order. This order lists the nodes one level at a time, from left to right. A null value marks a missing child. The input shows p and q by their values.
An ancestor of a node is the node itself or any node above it on the path to the root. The lowest common ancestor of p and q is the deepest node that is an ancestor of both p and q.
Return the lowest common ancestor node of p and q. The output shows the value of that node. All node values in the tree are different.
Example 1
- Input:
- root = [10,4,7,1,8,null,3,null,null,5,2], p = 5, q = 1
- Output:
- 4
- Explanation:
- Node 4 is above both node 5 and node 1. No deeper node is above both of them.
Example 2
- Input:
- root = [10,4,7,1,8,null,3,null,null,5,2], p = 8, q = 2
- Output:
- 8
- Explanation:
- Node 2 is a child of node 8. A node is an ancestor of itself, so the answer is node 8.
Example 3
- Input:
- root = [1,2], p = 1, q = 2
- Output:
- 1
Constraints
- 2 <= number of nodes <= 10^5
- -10^9 <= Node.val <= 10^9
- All Node.val values are unique.
- p != q
- p and q are in the tree.
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
- Step-By-Step Directions From a Binary Tree Node to Another
Coding · Binary Lifting · Binary Tree · Depth-First Search
- Lowest Common Ancestor of a Binary Tree III
Coding · Binary Lifting · Binary Tree · Hash Table
- Smallest Common Region
Coding · Array · Binary Lifting · Breadth-First Search
- All Nodes Distance K in Binary Tree
Coding · Binary Tree · Breadth-First Search · Depth-First Search
Sources · 2 references
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 10 records
Amazon
Leetcode 236. Lowest Common Ancestor of a Binary Tree
Reported Sep 30, 2026 · 5 source reports
MID_LEVEL, SENIOR
Source snapshot: Nov 12, 2026
View sourceApple
Leetcode 236. Lowest Common Ancestor of a Binary Tree
Reported Jun 29, 2026 · 2 source reports
MID_LEVEL
Source snapshot: Nov 12, 2026
View sourceAtlassian
Leetcode 236. Lowest Common Ancestor of a Binary Tree
Reported Dec 18, 2025 · 3 source reports
PRINCIPAL, STAFF
Source snapshot: Nov 12, 2026
View sourceCanva
Leetcode 236. Lowest Common Ancestor of a Binary Tree
Reported Apr 28, 2025 · 1 source report
MID_LEVEL
Source snapshot: Nov 12, 2026