Questions tagged [least-common-ancestor]
37 questions
190
votes
34 answers
How to find the lowest common ancestor of two nodes in any binary tree?
The Binary Tree here is may not necessarily be a Binary Search Tree.
The structure could be taken as -
struct node {
int data;
struct node *left;
struct node *right;
};
The maximum solution I could work out with a friend was something…

Siddhant
- 2,535
- 4
- 20
- 22
20
votes
3 answers
Determine least common ancestor at compile-time
There are tons of questions about the least common ancestor algorithm, but this one is different because I'm trying to determine the LCA at compile-time, and my tree is neither binary nor a search tree, even though my simplified version may look…

Nicolas Holthaus
- 7,763
- 4
- 42
- 97
12
votes
6 answers
Lowest Common Ancestor Algorithm
So I have been looking into implementing a lowest common ancestor algorithm. I looked at many different algorithms (mainly variations of Trajan's solution or variations of the RMQ).
I am using a non-binary tree. My tree will often change between…

slimbo
- 2,699
- 4
- 25
- 36
10
votes
1 answer
Range Minimum Query approach (from tree to restricted RMQ)
So, I read this TopCoder tutorial on RMQ (Range Minimum Query), and I got a big question.
On the section where he introduced the approach, what I can understand until now is this:
(The whole approach actually uses methodology introduced in Sparse…

Shane Hsu
- 7,937
- 6
- 39
- 63
9
votes
4 answers
How do criss-cross merges arise in Git?
I have been going through the, "git merge-base", man page and I can't understand how multiple merge bases develop. Specifically, I'm hung up on the following illustration in the man page:
When the history involves criss-cross merges, there can be…

MACS
- 163
- 1
- 4
8
votes
2 answers
How to finding first common ancestor of a node in a binary tree?
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))
…

dojoBeginner
- 449
- 2
- 8
- 15
7
votes
1 answer
How to represent a non binary tree and how to do LCA on that tree?
How are non binary trees typically represented? Trees where there is no limit to the number of children a node can have. Is it best to use a Adjacency Matrix or Adjacency List and just assume there will be no cycles, or do something similar to…

drop27
- 143
- 2
- 11
6
votes
3 answers
What are the practical applications of the lowest common ancestor algorithms?
I was having a look at this question and then reading about Tarjan's least common ancestors algorithm. I never came across any applications of LCA algorithms before.
Where are such LCA algorithms commonly used?

Lazer
- 90,700
- 113
- 281
- 364
5
votes
2 answers
Find multiple LCAs in unrooted tree
I am trying to implement LCA for unrooted tree. I have given a tree (a connected undirected graph without cycles) and a sequence of queries about LCA for some root and two vertices. Every particular query can have different root, so I cannot use…

ciechowoj
- 914
- 6
- 26
4
votes
2 answers
Why is this implicit cast is not possible?
Given classes and interfaces below, I am wondering why implicit cast:
ISomeModelAbstract x = new ConcreteClass();
Is impossible. I tried
public interface ISomeModelAbstract where T: IBasicModel
But then I cannot use GetById…

Node.JS
- 1,042
- 6
- 44
- 114
4
votes
2 answers
How to compute a least common ancestor algorithm's time complexity?
I came into an article which talking about the LCA algorithms, the code is simple
http://leetcode.com/2011/07/lowest-common-ancestor-of-a-binary-tree-part-i.html
// Return #nodes that matches P or Q in the subtree.
int countMatchesPQ(Node *root,…

bigpotato
- 211
- 2
- 3
- 13
4
votes
2 answers
Least common ancestor of multiple nodes in DAG
How can I find a least common ancestors of multiple nodes in a directed acyclic graph?
I've found quite a few papers on the topic but they all seem to find LCAs in DAG for two nodes.
Are there good algorithms for multiple nodes?

yper
- 41
- 1
- 3
3
votes
1 answer
Finding best common ancestor of two leaf nodes where nodes have zero, one, or two parents
Goal:
I am looking for an algorithm to find the best common ancestor of a graph where nodes in the graph can have zero, one, or two parents. I am not sure of the terminology of "best common ancestor": better terminology might be "lowest common…

bgoodr
- 2,744
- 1
- 30
- 51
3
votes
2 answers
LCA problem at interview
Sometimes I come across interview questions like this: "Find the common parent of any 2 nodes in a tree". I noticed that they ask LCA questions also at Google, Amazon, etc.
As wikipedia says LCA can be found by intersection of the paths from the…

Michael
- 41,026
- 70
- 193
- 341
3
votes
3 answers
Is there a better way to find lowest common ancestor?
I know similar questions have been asked before, but I think my solution is far simpler. Especially compared to Wikipedia.
Please prove me wrong!
If you have a tree with nodes that have the given data structure:
struct node
{
node * left;
…

theninjagreg
- 1,616
- 2
- 12
- 17