GeeksforGeeks » Trees specific questions
Ancestor of two given leaf nodes
(7 posts)-
Determine the first common ancestor of twi given leaf nodes in Binary Tree.
-
This can be solved as follows:
1. keep traversing from the root node and check in which subtree each of the two node lies in
2. if both lie in different subtrees than this node is the least common ancestor.
3. If both lie in the same subtree then recursively call this method in that subtree.will search each of the two nodes in the left tree and the right tree. Till the point where they are in the same half keep doing this for the child nodes and at the point where the two nodes are in different halves that is the nearest ancestor
The definition of the TreeNode
public class TreeNode
{
public int Data;
public TreeNode LeftChildNode;
public TreeNode RightChildNode;
}
// The implementation of the method to get the nearest common acncestor
TreeNode NearestCommonAncestor(TreeNode rootNode, TreeNode firstNode, TreeNode secondNode)
{
if (rootNode == NULL || firstNode == NULL || secondNode == NULL)
{
return NULL;
}
if (IsNodePresent(root.LeftChild, firstNode) && IsNodePresent(root.LeftChild, secondNode))
{
//if both are present in the left child of the root then call this method on the left child
return NearestCommonAncestor(rootNode.LeftChild, firstNode, secondNode);
}
if (IsNodePresent(root.RightChild, firstNode) && IsNodePresent(root.RightChild, secondNode))
{
//if both the nodes are present in the right child then call this method on the right child
return (NearestCommonAncestor(root.RightChild, firstNode secondNode);
}
//each node is present in different subtrees hence the current rootNode is the nearest common ancestor
return rootNode;
}
//The implementation of IsNodePresent method which checks if a node is present in the current subtree
Bool IsNodePresent(TreeNode rootNode, TreeNode targetNode)
{
if (rootNode == NULL || targetNode == NULL)
{
return false;
}
if (rootNode.Data == targetNode.Data)
{
return true;
}
if (IsNodePresent(rootNode.LeftChild, targetNode))
{
return true;
}
if (IsNodePresent(rootNode.RightChild, targetNode))
{
return true;
}
return false;
} -
@anand can u explain done in code why u have used euler tour whats its purpose also how u calculated LCA .??????
reply asap
-
It was published on the following link
-
Euler tour is need to find number of nodes between the u and v for which common ancestor has to be calculated. LCA problem can be reduce to Range Minimum Query(RMQ). as we need to find the node situated at lowest level among the nodes that are obtained between u and v from Euler tour.
Let me know if it still not clear
-
int LCA(node *t,int a,int b)
{ if((t->info > a && t->info < b)|| t->info==a || t->info==b )
{ cout<<"found "<<t->info;
return t->info; }
else if(t->info > a && t->info > b )
{ cout<<t->info<<"going left "; return LCA( t->left,a,b); }
else if(t->info < a && t->info < b )
{ cout<<t->info<<"going left "; return LCA( t->right,a,b); }
else return -1;
}
Reply
You must log in to post.