Following is my algorithm to find first common ancestor. But I don’t know how to calculate it time complexity, can anyone help?
public Tree commonAncestor(Tree root, Tree p, Tree q) {
if (covers(root.left, p) && covers(root.left, q))
return commonAncestor(root.left, p, q);
if (covers(root.right, p) && covers(root.right, q))
return commonAncestor(root.right, p, q);
return root;
}
private boolean covers(Tree root, Tree p) { /* is p a child of root? */
if (root == null) return false;
if (root == p) return true;
return covers(root.left, p) || covers(root.right, p);
}
As hammar’s answer demonstrates, your algorithm is quite inefficient as many operations are repeated.
I would do a different approach: Instead of testing for every potential root node if the two given nodes are not in the same sub-tree (thus making it the first common ancestor) I would determine the the paths from the root to the two given nodes and compare the nodes. The last common node on the paths from the root downwards is then also the first common ancestor.
Here’s an (untested) implementation in Java:
Both
pathToNode
andcommonAncestor
are in O(n).Ok, so let's start by identifying what the worst case for this algorithm would be.
covers
searches the tree from left to right, so you get the worst-case behavior if the node you are searching for is the rightmost leaf, or it is not in the subtree at all. At this point you will have visited all the nodes in the subtree, socovers
is O(n), where n is the number of nodes in the tree.Similarly,
commonAncestor
exhibits worst-case behavior when the first common ancestor ofp
andq
is deep down to the right in the tree. In this case, it will first callcovers
twice, getting the worst time behavior in both cases. It will then call itself again on the right subtree, which in the case of a balanced tree is of sizen/2
.Assuming the tree is balanced, we can describe the run time by the recurrence relation
T(n) = T(n/2) + O(n)
. Using the master theorem, we get the answerT(n) = O(n)
for a balanced tree.Now, if the tree is not balanced, we might in the worst case only reduce the size of the subtree by 1 for each recursive call, yielding the recurrence
T(n) = T(n-1) + O(n)
. The solution to this recurrence isT(n) = O(n^2)
.You can do better than this, though.
For example, instead of simply determining which subtree contains
p
orq
withcover
, let's determine the entire path top
andq
. This takesO(n)
just likecover
, we're just keeping more information. Now, traverse those paths in parallell and stop where they diverge. This is alwaysO(n)
.If you have pointers from each node to their parent you can even improve on this by generating the paths "bottom-up", giving you
O(log n)
for a balanced tree.Note that this is a space-time tradeoff, as while your code takes
O(1)
space, this algorithm takesO(log n)
space for a balanced tree, andO(n)
space in general.