Lowest Common Ancestor of a Binary Tree
CodingPhone ScreenSoftware EngineerReported Feb, 2026
Examples
Example 1:
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Output: 3
Example 1:
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Output: 3
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.
2 <= number of nodes <= 10^5p and q exist in the tree.Reported as LeetCode 236 in a software engineer phone screen.