Lowest Common Ancestor of a Binary Tree Part I
July 18, 2011 in binary tree
Given a binary tree, find the lowest common ancestor of two given nodes in the tree.
_______3______ / \ ___5__ ___1__ / \ / \ 6 _2 0 8 / \ 7 4
If you are not so sure about the definition of lowest common ancestor (LCA), please refer to my previous post: Lowest Common Ancestor of a Binary Search Tree (BST) or the definition of LCA here. Using the tree above as an example, the LCA of nodes 5 and 1 is 3. Please note that LCA for nodes 5 and 4 is 5.
Hint:
Top-down or bottom-up? Consider both approaches and see which one is more efficient.
A Top-Down Approach (Worst case O(n2) ):
Let’s try the top-down approach where we traverse the nodes from the top to the bottom. First, if the current node is one of the two nodes, it must be the LCA of the two nodes. If not, we count the number of nodes that matches either p or q in the left subtree (which we call totalMatches). If totalMatches equals 1, then we know the right subtree will contain the other node. Therefore, the current node must be the LCA. If totalMatches equals 2, we know that both nodes are contained in the left subtree, so we traverse to its left child. Similar with the case where totalMatches equals 0 where we traverse to its right child.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | // Return #nodes that matches P or Q in the subtree. int countMatchesPQ(Node *root, Node *p, Node *q) { if (!root) return 0; int matches = countMatchesPQ(root->left, p, q) + countMatchesPQ(root->right, p, q); if (root == p || root == q) return 1 + matches; else return matches; } Node *LCA(Node *root, Node *p, Node *q) { if (!root || !p || !q) return NULL; if (root == p || root == q) return root; int totalMatches = countMatchesPQ(root->left, p, q); if (totalMatches == 1) return root; else if (totalMatches == 2) return LCA(root->left, p, q); else /* totalMatches == 0 */ return LCA(root->right, p, q); } |
What is the run time complexity of this top-down approach?
First, just for fun, we assume that the tree contains n nodes and is balanced (with its height equals to log(n) ). In this case, the run time complexity would be O(n). Most people would guess a higher ordered complexity than O(n) due to the function countMatchesPQ() traverses the same nodes over and over again. Notice that the tree is balanced, you cut off half of the nodes you need to traverse in each recursive call of LCA() function. The proof that the complexity is indeed O(n) is left as an exercise to the reader.
What if the tree is not necessarily balanced? Then in the worst case the complexity could go up to O(n2). Why? Could you come up with such case? (Hint: The tree might be a degenerate tree).
A Bottom-up Approach (Worst case O(n) ):
Using a bottom-up approach, we can improve over the top-down approach by avoiding traversing the same nodes over and over again.
We traverse from the bottom, and once we reach a node which matches one of the two nodes, we pass it up to its parent. The parent would then test its left and right subtree if each contain one of the two nodes. If yes, then the parent must be the LCA and we pass its parent up to the root. If not, we pass the lower node which contains either one of the two nodes (if the left or right subtree contains either p or q), or NULL (if both the left and right subtree does not contain either p or q) up.
Sounds complicated? Surprisingly the code appears to be much simpler than the top-down one.
1 2 3 4 5 6 7 8 | Node *LCA(Node *root, Node *p, Node *q) { if (!root) return NULL; if (root == p || root == q) return root; Node *L = LCA(root->left, p, q); Node *R = LCA(root->right, p, q); if (L && R) return root; // if p and q are on both sides return L ? L : R; // either one of p,q is on one side OR p,q is not in L&R subtrees } |
Notes:
The LCA problem had been studied extensively by many computer scientists. There exists efficient algorithms for finding LCA in constant time after initial processing of the tree in linear time. For the adventurous reader, please read this article for more details: Range Minimum Query and Lowest Common Ancestor in Topcoder.
Further Thoughts:
What if each node in the binary tree has a link to its parent? Could you devise a non-recursive approach without using extra space?
» Continue reading Lowest Common Ancestor of a Binary Tree Part II.
Lowest Common Ancestor of a Binary Tree Part I,
www said on July 22, 2011
“we assume that the tree contains n nodes and is balanced (with its height equals to log(n) ). In this case, the run time complexity would be O(n). ”
i think this is similar as O(n) time in making a heap..the prove can be found in CLRS page135
Eugene said on May 17, 2012
how can you see?
Venkata Gopi said on December 7, 2012
What if one of the search node not present in tree? Still this(bottom up algy) code works?
Sean said on July 25, 2011
One C++ question here:
If you are having Node* root, Node* p here, how did you overload the “==” operator to directly use them like if(root == p)?
1337c0d3r said on July 25, 2011
All pointers in C++ are just 32-bit unsigned integer values, so you don’t need to overload the “==” operator to do comparison.
Sean said on July 25, 2011
I see, so the assumption here is that the nodes pointed by p and q should be exactly the same objects to be found in the binary tree.
zorrowang said on September 15, 2011
I think there is a small bug in Top-Down Approach. According to the definition of LCA, it allows a node to be a descendant of itself. So when node p = q (p or q is not the root), the LCA should be itself. But your approach figures out the root as the LCA.
vycon said on October 1, 2011
What is the running time of the bottom-up approach please?
zhong zhang said on February 5, 2012
I think the code works well.
——root——–
/ \
p and q null
The code will return p or q.
hbs said on November 5, 2011
O(n)
Liping said on November 20, 2011
Genius!!
learner said on November 30, 2011
I don’t think the bottom-up approach code is *correct*, in the sample tree, find LCA(root, 5, 9) returns 5, it should be null.
MSJ said on January 23, 2012
i don’t think it correct either, LCA(root,5,4) return 5 but its internal logic does not reflect finding node 4 there. aslo LCA(root,5,NULL) also return 5
zhong zhang said on February 5, 2012
Given a binary tree, find the lowest common ancestor of “two given nodes in the tree”.
The two nodes must be in the tree according to the question.
Lee said on February 7, 2013
If both the nodes are already found in one of the subtrees of root, it does traversal again in vain. May be a static variable will be suffice to check, if subtree traversal is required or not
Nagarjuna said on February 2, 2012
Will the bottom up approach work if there are duplicate elements in the tree, and we are looking for LCA of elements with one of them being duplicate?
Alex said on February 20, 2012
I think it doesn’t matter. Since “given nodes in the tree” means pointer, not the value in the tree
wael said on May 3, 2012
you are assuming that both nodes exist in the tree which is not always the case.
sachin said on May 14, 2012
can you please tell me,how to find out the lca of two given nodes in n-ary tree?
linda said on May 26, 2012
if p or q is not in the tree, this will still return the ancestor of the existing one
we won’t touch both p and q because of this line
“if (root == p || root == q) return root;”
consider a tree like this
1
/ \
3 4
\
7
/ \
6 8
let p = 4 q = 8, when we reach 4, we will return, and 8 will never be visited.
Sanjay Pandey said on June 20, 2012
no this will work correctly .
first 8 would we visited then as LCA(root->left, p, q); will go left then after traversing left subtree of 1 will visit right subtree and then following condn hlds
if(L && R)
return root
Mridul said on June 11, 2012
The bottom up approach has one bug.
For eg: let us assume the tree is
inorder : 1 3 4 5 8 9 10 15
preorder : 10 8 4 3 1 5 9 15
try to find the lca(4,40) :- function will return 4 as the answer, which is incorrect as 40 is not at all in the tree.
this error occurs whenever there will be one of p or q present in the tree and other is not, because in line 2, we check that if root’s value is either equal to p or q, then return and we didn’t check for the other number.
I try to solve this problem…but can’t able to fix this…….so guys help me in fixing this issue…
thanks 1337c03dr for ur nice solutioin..
Sanjay Pandey said on June 20, 2012
the bottom up approach won’t work if the binary tree has duplicate element.
suppose there are two 2′s and LCA is to be calculated of (2,10) as —
5
/ \
3 6
/ \
2 2
/ \
8 7
it would return add of 3
correct if i am wrong
Kamal said on August 22, 2012
LCA of a Binary Search Tree is easier as shown here,
http://www.ritambhara.in/lowest-common-ancestor-in-binary-search-tree/
EddieZ said on August 26, 2012
@Mridul, to solve the problem of one of node is not in the tree
int LCA(N* n, N* a, N* b, N*& head)
{
if(!n || !a || !b) return 0;
int l = LCA1(n->l, a, b, head);
int r = LCA1(n->r, a, b, head);
if(l==2 || r == 2)
return 2;
else if((l==1 && r == 1)
||( (l == 1 || r == 1 ) && (n == a || n == b)) )
{
head = n;
return 2;
}
else if(n==a || n == b)
return 1;
else
return max(l,r);
}
Hitman said on August 28, 2012
i think if the problem with the bottom up algo is just the case when one of the nodes doesn’t exist, then we can just do a pre check for the existance of both nodes in o(N), by calling the counting function of the top-down approach on the root, and checking if the return value is 2.
babycandy said on September 3, 2012
agree with Sanjay Pandey
the bottom up approach won’t work if the binary tree has duplicate element.
zhong zhang said on September 23, 2012
For babycandy and Sanjay, maybe you guys misunderstand the problem.
In the problem, we just consider the node itself(not the data element of the node), which is a pointer. In Sanjay’s example, it’s not duplicated. The two nodes with data element 2 are different nodes.
Subramanian Ganapathy said on October 15, 2012
Top down worst case O(n)
DFS and return the stack of nodes from bottom to root of all nodes in the path of each node{first adn second}.
Then do a merge like procedure by popping off the excess elements of one stack and then successively popping until both the stacks have the same top.
Venkata Gopi said on December 7, 2012
What if one of the search node not present in tree? Still this(bottom up algy) code work?
rohit said on February 25, 2013
My attempt though in C# this solves the extreme edge case of one of the nodes not present in tree while keeping botom up approach intact
public Node LCA(Node Root,Noe P,Node Q,Ref bool Pbool,ref bool Qbool)
{
if (Root==null||P==null||Q==null)
L = LCA(Root.Left,P,Q, ref Pbool, ref Qbool);
R = LCA(Root.Left,P,Q, ref Pbool, ref Qbool);
if(P==Root)
Pbool = true; // identify if both the values are identified in the tree before spitting out LCA
Return Root;
if (Q ==Root)
Qbool = true; // identify if both the values are identified in the tree before spitting out LCA
Return Root;
Return L==null?R:L;
}
Gordon said on April 6, 2013
To 1337c0d3r:
1. Assuming 2 nodes already in tree. This question has been asked by a lot of people not only here but also in the one for BST. You should put it clearly in your post that you make this assumption or better yet, do not make this assumption and provide code works for these cases.
2. You are using the return value is null or not as an indicator if you found at least one value in one side of the tree. Simply change it to a structure that has a node and 2 boolean values, you can know if you found one node and which node or both nodes on one side of the tree. And you can avoid call the other side of the tree.
jim said on April 7, 2013
for the Bottom-up Approach, if p is not in the tree, but q is, it returns the root as LCA, which does not make sense. Should it return null in this case?