Recursive descent: if both subtrees return truthy at a node, that node is the LCA.
Given a binary tree and two node values p and q, find their Lowest Common Ancestor: the deepest node that is an ancestor of both. The algorithm performs a single recursive DFS that propagates the found nodes upward; no parent pointers or extra storage are needed.
At every node we check three things: is the node null (base case: return null)? Is the node p or q (return it immediately)? Otherwise recurse into both subtrees and collect their results.
When a node's left subtree returns non-null AND its right subtree returns non-null, that node is the LCA. Each subtree found one of {p, q}, which means this node sits at the crossroads. If only one side returns non-null, the result is simply bubbled up, because both targets are in the same subtree.
The algorithm visits every node at most once, so time is O(n). The recursion stack grows with the height of the tree, so it is O(h). For a balanced tree h = O(log n); for a skewed tree h = O(n). No extra data structures are needed beyond the call stack itself.
This single-pass approach assumes both p and q exist in the tree. If either might be absent, a two-pass strategy (find both nodes first, then LCA) is safer. The same pattern generalises to n-ary trees and forests.