Queries to calculate sum of the path from root to a given node in given Binary Tree | Function to find the sum of the path from root to the current node ; Sum of nodes in the path ; Iterate until root is reached ; Update the node value ; Print the resultant sum ; Function to print the path sum for each query ; Traverse the queries ; arraylist to store integers
function sumOfNodeInAPath ( node_value ) { let sum_of_node = 0 ; while ( node_value ) { sum_of_node += node_value ; node_value = Math . floor ( node_value / 2 ) ; } document . write ( sum_of_node ) ; return ; } function findSum ( Q ) { for ( let i = 0 ; i < Q . length ; i ++ ) { let node_value = Q [ i ] ; sumOfNodeInAPath ( node_value ) ; document . write ( " " ) ; } } let arr = [ 1 , 5 , 20 , 100 ] ; findSum ( arr ) ;