## 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(n ^{2}) ):**

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(*n*^{2}). 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.**

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

+2Eugene said on May 17, 2012

how can you see？

0sandy said on December 23, 2013

find left count

if left count == 2 => go left

if left count ==1 => find right count (cant take it for granted that the other node is also present)

if left count ==0 => find right count

so worst case we traversed n nodes

but we reduce the sub problem by half if both the nodes are present on left or right side

so we get number of operations in each recursion as

n + n/2 + n/4 + .. 1

= n(1+1/2+1/4+… 1/2^lgn)

as we know 1 +1/2 +1/4 +… inifinity = 2

we get n*k where k>1 but <2

so T(n) for top down approach = O(n)

+2Venkata Gopi said on December 7, 2012

What if one of the search node not present in tree? Still this(bottom up algy) code works?

-1Sean 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)?

01337c0d3r 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.

0Sean 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.

0zorrowang 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.

+2vycon said on October 1, 2011

What is the running time of the bottom-up approach please?

0zhong zhang said on February 5, 2012

I think the code works well.

——root——–

/ \

p and q null

The code will return p or q.

0hbs said on November 5, 2011

O(n)

0Liping said on November 20, 2011

Genius!!

0learner 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.

0MSJ 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

0zhong 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.

+1Lee 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

0ab123 said on September 18, 2013

I think the input in this question is two nodes whose LCA we need to find. So, the case where one of the node is incorrect does not arise. Thus, i think the solution is correct only.

+1Nagarjuna 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?

0Alex 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

0wael said on May 3, 2012

you are assuming that both nodes exist in the tree which is not always the case.

0sachin said on May 14, 2012

can you please tell me,how to find out the lca of two given nodes in n-ary tree?

+1linda 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.

0Sanjay 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

0Mridul 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..

0Sanjay 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

0Kamal 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/

0EddieZ 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);

}

0Hitman 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.

+1babycandy said on September 3, 2012

agree with Sanjay Pandey

the bottom up approach won’t work if the binary tree has duplicate element.

0zhong 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.

0Subramanian 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.

+1Venkata Gopi said on December 7, 2012

What if one of the search node not present in tree? Still this(bottom up algy) code work?

0rohit 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;

}

0Gordon 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.

0Dan said on August 7, 2013

re. 2: it should be fine, because it avoids traversing further in one side of tree by traversing other side of tree instead.

0jim 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?

0Di said on August 16, 2013

If the p and q are the same pointer. The result will be wrong.Actually.

Di

0deb said on September 13, 2013

The following code takes care of all the cases.. Enjoy.

+1Ming said on November 27, 2013

I have one simple approach which is based on post-order of binary tree traversing.

struct tree {

struct tree *l, *r;

int key;

};

static int n1 = 0, n2 = 0; //0: the given node not visited, 1: the given node visted

static struct tree *a1, *a2; //give two nodes

static void visit(struct tree *t)

{

if (t == a1)

n1 = 1;

if (t == a2)

n2 = 1;

}

void lca(struct tree *root)

{

int a1, a2;

a1 = n1;

a2 = n2;

if (!root) return;

lca(root->l);

lca(root->r);

visit(root);

if (!a1 && !a2 && n1 && n2) {

printf(“lca of n1 & n2 is node %d\n”, root->key);

exit(0)

}

}

0Sean said on May 8, 2014

The above solution of the top-down apporach has one bug in countMatchesPQ() when p and q are equal nodes. When p equals q, the LCA is p/q itself.

However, the above top-down solution will return the parent of p/q. Thus, countMatchesPQ() should be modified as below.

0Lal Shah said on July 4, 2014

Start from root node and move downward.

If any node found has either p or p as its direct child then it is the LCA.

If a node is found with p in its left(or right) subtree and q in its right(or left) subtree then it is the LCA.

For explanation and code http://www.algoqueue.com/algoqueue/default/view/8650752/find-lowest-common-ancestor-in-a-binary-tree

0XBYY said on July 30, 2014

The Bottom-up method will not work if one of the nodes is not present inside the tree, I think we can use a variable for flag to represent which node have been alread found.

0