← 返回 uber 的题目列表Lowest Common Ancestor of a Binary Tree
类型:qbank
Given the root of a binary tree and two nodes p and q, return their lowest common ancestor.
Lowest Common Ancestor of a Binary Tree
Given the root of a binary tree and two nodes p and q, return their lowest common ancestor.
SWE
tree
dfs
recursion
medium
Frequency
Single report
Last asked
2026-02-26
Stage
phone-screen
Lowest Common Ancestor of a Binary Tree
Given the root of a binary tree and two nodes p and q, return their lowest common ancestor.
The lowest common ancestor is the lowest node in the tree that has both p and q as descendants, where a node may be a descendant of itself.
Examples
Example 1:
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Output: 3
Constraints
2 <= number of nodes <= 10^5
All node values are unique.
p and q exist in the tree.