Graph Theory37 sections · 1633 units
Open in Course

Problem - LCA of a Binary Tree

(LeetCode 236)

LeetCode 236236 asks you to find the lowest common ancestor of 22 nodes in a binary tree. You are given a binary tree root and 22 target nodes p and q. Return the deepest node that is an ancestor of both. Every node is considered its own ancestor, so if p is an ancestor of q, then p is the LCA.

This problem tests your ability to write a clean recursive solution. You do not need binary lifting or Euler tour here because you only answer a single query on a moderately sized tree. The recursive approach runs in O(n)O(n) time and O(h)O(h) space, which is efficient enough.