Queries to calculate sum of the path from root to a given node in given Binary Tree | package whatever do not write package name here ; 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 ; Driver Code ; arraylist to store integers
import java . io . * ; import java . util . ArrayList ; class GFG { public static void sumOfNodeInAPath ( int node_value ) { int sum_of_node = 0 ; while ( node_value > 0 ) { sum_of_node += node_value ; node_value /= 2 ; } System . out . print ( sum_of_node ) ; } public static void findSum ( ArrayList < Integer > Q ) { for ( int i = 0 ; i < Q . size ( ) ; i ++ ) { int node_value = Q . get ( i ) ; sumOfNodeInAPath ( node_value ) ; System . out . print ( " ▁ " ) ; } } public static void main ( String [ ] args ) { ArrayList < Integer > arr = new ArrayList < > ( ) ; arr . add ( 1 ) ; arr . add ( 5 ) ; arr . add ( 20 ) ; arr . add ( 100 ) ; findSum ( arr ) ; } }