Sum of all odd nodes in the path connecting two given nodes | Binary Tree node ; Utitlity function to create a new Binary Tree node ; Function to check if there is a path from root to the given node . It also populates ' arr ' with the given path ; if root is null there is no path ; push the node ' s ▁ value ▁ in ▁ ' arr ' ; if it is the required node return true ; else check whether the required node lies in the left subtree or right subtree of the current node ; required node does not lie either in the left or right subtree of the current node Thus , remove current node ' s ▁ value ▁ from ▁ ▁ ' arr 'and then return false ; Function to get the sum of odd nodes in the path between any two nodes in a binary tree ; vector to store the path of first node n1 from root ; vector to store the path of second node n2 from root ; Get intersection point ; Keep moving forward until no intersection is found ; calculate sum of ODD nodes from the path ; Driver Code
class Node { constructor ( data ) { this . left = null ; this . right = null ; this . data = data ; } } function newNode ( data ) { let node = new Node ( data ) ; return node ; } function getPath ( root , arr , x ) { if ( root == null ) return false ; arr . push ( root . data ) ; if ( root . data == x ) return true ; if ( getPath ( root . left , arr , x ) || getPath ( root . right , arr , x ) ) return true ; arr . pop ( ) ; return false ; } function sumOddNodes ( root , n1 , n2 ) { let path1 = [ ] ; let path2 = [ ] ; getPath ( root , path1 , n1 ) ; getPath ( root , path2 , n2 ) ; let intersection = - 1 ; let i = 0 , j = 0 ; while ( i != path1 . length j != path2 . length ) { if ( i == j && path1 [ i ] == path2 [ j ] ) { i ++ ; j ++ ; } else { intersection = j - 1 ; break ; } } let sum = 0 ; for ( i = path1 . length - 1 ; i > intersection ; i -- ) if ( path1 [ i ] % 2 != 0 ) sum += path1 [ i ] ; for ( i = intersection ; i < path2 . length ; i ++ ) if ( path2 [ i ] % 2 != 0 ) sum += path2 [ i ] ; return sum ; } let root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; root . right . right = newNode ( 6 ) ; let node1 = 5 ; let node2 = 6 ; document . write ( sumOddNodes ( root , node1 , node2 ) ) ;