Find distance between two nodes in the given Binary tree for Q queries | C # program to find distance between two nodes using LCA ; log2 ( MAX ) ; Array to store the level of each node ; List to store tree ; Pre - Processing to calculate values of lca [ , ] , dist [ , ] ; Using recursion formula to calculate the values of lca [ , ] ; Storing the level of each node ; Using recursion formula to calculate the values of lca [ , ] and dist [ , ] ; Function to find the distance between given nodes u and v ; The node which is present farthest from the root node is taken as v . If u is farther from root node then swap the two ; Finding the ancestor of v which is at same level as u ; Adding distance of node v till its 2 ^ i - th ancestor ; If u is the ancestor of v then u is the LCA of u and v ; Finding the node closest to the root which is not the common ancestor of u and v i . e . a node x such that x is not the common ancestor of u and v but lca [ x , 0 ] is ; Adding the distance of v and u to its 2 ^ i - th ancestor ; Adding the distance of u and v to its first ancestor ; Driver Code ; Number of nodes ; Add edges with their cost ; Initialising lca and dist values with - 1 and 0 respectively ; Perform DFS ; Query 1 : { 1 , 3 } ; Query 2 : { 2 , 3 } ; Query 3 : { 3 , 5 }
using System ; using System . Collections . Generic ; class GFG { static readonly int MAX = 1000 ; static readonly int log = 10 ; static int [ ] level = new int [ MAX ] ; static int [ , ] lca = new int [ MAX , log ] ; static int [ , ] dist = new int [ MAX , log ] ; static List < List < int [ ] > > graph = new List < List < int [ ] > > ( ) ; static void addEdge ( int u , int v , int cost ) { graph [ u ] . Add ( new int [ ] { v , cost } ) ; graph [ v ] . Add ( new int [ ] { u , cost } ) ; } static void dfs ( int node , int parent , int h , int cost ) { lca [ node , 0 ] = parent ; level [ node ] = h ; if ( parent != - 1 ) { dist [ node , 0 ] = cost ; } for ( int i = 1 ; i < log ; i ++ ) { if ( lca [ node , i - 1 ] != - 1 ) { lca [ node , i ] = lca [ lca [ node , i - 1 ] , i - 1 ] ; dist [ node , i ] = dist [ node , i - 1 ] + dist [ lca [ node , i - 1 ] , i - 1 ] ; } } foreach ( int [ ] i in graph [ node ] ) { if ( i [ 0 ] == parent ) continue ; dfs ( i [ 0 ] , node , h + 1 , i [ 1 ] ) ; } } static void findDistance ( int u , int v ) { int ans = 0 ; if ( level [ u ] > level [ v ] ) { int temp = u ; u = v ; v = temp ; } for ( int i = log - 1 ; i >= 0 ; i -- ) { if ( lca [ v , i ] != - 1 && level [ lca [ v , i ] ] >= level [ u ] ) { ans += dist [ v , i ] ; v = lca [ v , i ] ; } } if ( v == u ) { Console . WriteLine ( ans ) ; } else { for ( int i = log - 1 ; i >= 0 ; i -- ) { if ( lca [ v , i ] != lca [ u , i ] ) { ans += dist [ u , i ] + dist [ v , i ] ; v = lca [ v , i ] ; u = lca [ u , i ] ; } } ans += dist [ u , 0 ] + dist [ v , 0 ] ; Console . WriteLine ( ans ) ; } } public static void Main ( String [ ] args ) { int n = 5 ; for ( int i = 0 ; i < MAX ; i ++ ) { graph . Add ( new List < int [ ] > ( ) ) ; } addEdge ( 1 , 2 , 2 ) ; addEdge ( 1 , 3 , 3 ) ; addEdge ( 2 , 4 , 5 ) ; addEdge ( 2 , 5 , 7 ) ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 0 ; j < log ; j ++ ) { lca [ i , j ] = - 1 ; dist [ i , j ] = 0 ; } } dfs ( 1 , - 1 , 0 , 0 ) ; findDistance ( 1 , 3 ) ; findDistance ( 2 , 3 ) ; findDistance ( 3 , 5 ) ; } }