Skip to content

#236 - Lowest Common Ancestor of a Binary Tree

MediumBinary Tree - DFS
Open on LeetCode

Given a binary tree and two nodes p and q, find their lowest common ancestor (LCA). The LCA is the deepest node that is an ancestor of both p and q (a node can be an ancestor of itself).

InputOutput
root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 13
root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 45
root = [1,2], p = 1, q = 21

Released under the MIT License.