# Lowest Common Ancestor in Binary Tree

• November 20th, 2012, 10:43 AM
ueg1990
Lowest Common Ancestor in Binary Tree
Following is my implementation of lowest common ancestor for a Binary Search tree. I have two question:

1) The time complexity is O(n) and space complexity is O(n) worse case but O(logn) average case for both time and space if BST is balanced. is that correct?
2) How can i extend my solution to binary trees and not just binary search trees.

Hope to hear from you soon.
Code Java:

``` //The function findOrQueue is to enqueue all elements upto target node to a queue public void findOrQueue(Node target, Node top, LLQueue q) { int cmp = target.getData().compareTo(top.getData()); if(cmp == 0) { q.enqueue(top); return ; } else if (cmp < 0) { q.enqueue(top); findOrQueue(target, top.getLeftChild(),q); } else { q.enqueue(top); findOrQueue(target, top.getRightChild(),q); } }   public Node LCA(Node n1, Node n2) throws QueueEmptyException { LLQueue q1 = new LLQueue(); LLQueue q2 = new LLQueue(); findOrQueue(n1,getRoot(),q1); findOrQueue(n2,getRoot(),q2); Node t = null; while (!q1.isEmpty() && !q2.isEmpty()) { Node t1 = (Node)q1.dequeue(); Node t2 = (Node)q2.dequeue(); if(t1.getData() != t2.getData()) { return t; } else t = t1; } if(q1.isEmpty() && q2.isEmpty()) return null; else return t; }```
• November 20th, 2012, 12:50 PM
helloworld922
Re: Lowest Common Ancestor in Binary Tree
1. Sounds right.

2. Try building your traversal paths bottom-up instead of top-down. Should give the same time complexity, I think.
• November 20th, 2012, 02:04 PM
ueg1990
Re: Lowest Common Ancestor in Binary Tree
doesnt bottom-up mean, i will need a parent pointer??
• November 20th, 2012, 03:00 PM
helloworld922
Re: Lowest Common Ancestor in Binary Tree
Yes.

Otherwise, you'd have to do a full tree traversal (either depth-breadth-first or breadth-depth-first)
• November 20th, 2012, 03:30 PM
ueg1990
Re: Lowest Common Ancestor in Binary Tree
so lets stay i have a parent pointer, i still have to traverse to the two nodes whose LCA i am calculating...so how will bottom-up help me?? can u plz help me with this?

--- Update ---

or are we assuming we already know the location of the the two nodes and that all the values in the nodes are distinct??
• November 20th, 2012, 03:40 PM
helloworld922
Re: Lowest Common Ancestor in Binary Tree
The idea is to build the traversal path bottom-up since this way you don't have to do any unnecessary traversals which you would have to do with regular tree searching.

After you have the ancestry, you flip the order and go top-down as you're currently doing to find the lowest common ancestry.
• November 20th, 2012, 03:54 PM
ueg1990
Re: Lowest Common Ancestor in Binary Tree
can u plz give an example of how to do bottom up traversal?? i dont understand how to build traversal bottom-up without knowing where the node is??
• November 20th, 2012, 04:39 PM
helloworld922
Re: Lowest Common Ancestor in Binary Tree
Oh, I was assuming you did know where each node was because your algorithm works with nodes.

Without knowing that you'd have to do some kind of top-down tree traversal to find the node.
• November 20th, 2012, 08:37 PM
ueg1990
Re: Lowest Common Ancestor in Binary Tree
ok, so my findOrQueue function does that....it finds the node first and then as it does find it, it adds the others nodes in its path to a queue....which part r u referring to with respect to bottom-up approach??
• November 20th, 2012, 08:40 PM
helloworld922
Re: Lowest Common Ancestor in Binary Tree
You're findOrQueue function won't work for a generic tree. That should be the only function you need to change. I was just suggesting using a bottom-up strategy for the findOrQueue function.
• November 21st, 2012, 10:16 AM
ueg1990
Re: Lowest Common Ancestor in Binary Tree
ok, i have one question abt bottom-up traversal, do it go uptill leaves and then up to the nodes i am calculating LCA for? and from there i go to the root?
• November 21st, 2012, 11:06 AM
helloworld922
Re: Lowest Common Ancestor in Binary Tree
You basically just start at the node n1/n2, then keep adding their parent, until you reach the root.

n1 -> n1.parent -> n1.parent.parent -> ... -> root