In binary trees, for given two nodes a and b, the lowest common ancestor is the node of which both a and b are descendants. Here a node can be descendant of itself.

In the above image, if we consider two nodes 2 and 3 then their lowest common ancestor will be node 1.
Similarly, the lowest common ancestor of 4 and 5 will be 2 and that of 3 and 4 would be 1.
The lowest common ancestor is the common and shared descendant of both given nodes.
LCA is used in many other binary tree problems like in d...
Published on April 12, 2021 08:24