Lowest Common Ancestor of a Binary Tree
ID: 88; medium
Last updated
ID: 88; medium
Last updated
There are 4 general cases.
If root
is A
or B
, we directly return the root since both A
and B
are guaranteed to be in the tree. If the root
is one of them, then the other one must be its descendent.
If A
and B
are on the different sides of the root
, then the root
of the LCA.
If A
and B
are both in the left tree, then we find the LCA of the left tree.
If A
and B
are both in the right tree, then we find the LCA of the right tree.