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);
}
Ok, so let’s start by identifying what the worst case for this algorithm would be.
coverssearches 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, socoversis O(n), where n is the number of nodes in the tree.Similarly,
commonAncestorexhibits worst-case behavior when the first common ancestor ofpandqis deep down to the right in the tree. In this case, it will first callcoverstwice, 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
porqwithcover, let’s determine the entire path topandq. 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.