Questions tagged [least-common-ancestor]
34 questions
188
votes
35 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…
![](../../users/profiles/179729.webp)
Siddhant
- 2,495
- 4
- 19
- 22
19
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…
![](../../users/profiles/4052030.webp)
Nicolas Holthaus
- 6,800
- 3
- 39
- 79
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…
![](../../users/profiles/185114.webp)
slimbo
- 2,451
- 3
- 23
- 35
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…
![](../../users/profiles/789253.webp)
Shane Hsu
- 6,377
- 6
- 32
- 60
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))
…
![](../../users/profiles/723697.webp)
dojoBeginner
- 431
- 2
- 6
- 14
8
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…
![](../../users/profiles/1650542.webp)
MACS
- 153
- 1
- 4
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…
![](../../users/profiles/4454627.webp)
drop27
- 143
- 2
- 10
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?
![](../../users/profiles/113124.webp)
Lazer
- 79,569
- 109
- 264
- 349
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…
![](../../users/profiles/3026910.webp)
ciechowoj
- 834
- 4
- 22
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…
![](../../users/profiles/1834787.webp)
Node.JS
- 1,849
- 3
- 28
- 86
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,…
![](../../users/profiles/2534781.webp)
bigpotato
- 193
- 1
- 2
- 11
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…
![](../../users/profiles/257924.webp)
bgoodr
- 2,372
- 1
- 25
- 45
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…
![](../../users/profiles/521070.webp)
Michael
- 37,415
- 63
- 167
- 303
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;
…
![](../../users/profiles/491803.webp)
theninjagreg
- 1,558
- 2
- 11
- 17
3
votes
1 answer
modification to LCA code for binary tree to check if node is present in Java
I have this code, which calculates Least common Ancestor of given two nodes in a Binary tree.
Currently, it assumes both nodes are present. I can write a helper method just to check if the nodes are present and then call the LCABT method. That would…
![](../../users/profiles/1988876.webp)
brain storm
- 25,842
- 54
- 187
- 349