Lowest Common Ancestor of Binary Tree
Find the lowest node that has both p and q in its subtree (a node counts as its own ancestor).
Intuition
Ask each subtree: 'do you contain p or q?' If both children answer yes, the current node is the split point — the LCA. If only one does, the LCA lives on that side. The recursion returns whichever of p/q/LCA it finds.
Approach
Return what you find
lca(node): None if empty; node itself if it is p or q; else combine child results.
Both sides answer → split point
left and right both non-None means p and q are in different subtrees — node is the answer, and it propagates unchanged upward.
One side answers → forward it
The non-None side already holds either the LCA or the single found target (correct when one target is the other's ancestor).
Solution & live demo
Edge cases
Recursion stops at p without exploring below — p returns as its own LCA, correct by the definition.
Only that child returns non-None; the answer bubbles from within it.