Distance b/w two nodes in tree such as:
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
Ex.:
Distance b/w nodes 8 and 15 =>>6
( 8->4->2->1->3->7->15) 6 vertices
Distance b/w nodes 8 and 10=>>3
while(i!=j)
{
if(i>j)
{
i=i/2;
count++;
}
else
{
j=j/2;
count++;
}
}
where i is intial node and j is final node :)
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
Ex.:
Distance b/w nodes 8 and 15 =>>6
( 8->4->2->1->3->7->15) 6 vertices
Distance b/w nodes 8 and 10=>>3
while(i!=j)
{
if(i>j)
{
i=i/2;
count++;
}
else
{
j=j/2;
count++;
}
}
where i is intial node and j is final node :)
No comments:
Post a Comment