Loading section...

Lowest Common Ancestor (LCA)

Concepts: pyLCA, pyBSTLCA, pyLCALineage

The Lowest Common Ancestor of two nodes p and q is the deepest node that has both p and q as descendants. It is one of the most elegant recursive tree problems because the logic almost writes itself once you understand the return-from-recursion mindset: if my left subtree contains one of p or q, and my right subtree contains the other, then I am the LCA. If both are in my left subtree, the LCA is somewhere in the left subtree. Delegate. LCA for a General Binary Tree Walk through the logic: when we recurse, each call returns either None (neither p nor q found in this subtree), p (p found but not q), q (q found but not p), or the LCA node itself. At any internal node, if both left and right return non-None, that node must be the split point — the LCA. The first time both sides are non-None i