Graph Theory37 sections · 1633 units
Open in Course

Core Idea - Multiple Approaches Exist

(enabling array algorithms)

There are several ways to solve LCA. One approach converts the tree into an array using an Euler tour, then reduces LCA to a Range Minimum Query. I'll cover that technique later in this section.

For a single LCA query on a moderate tree, a simpler recursive approach works well. You recurse through both subtrees and combine results. If you find one target on each side, the current node is the LCA. This runs in O(n)O(n) time and O(h)O(h) space, and you can implement it in a few lines of code.