Queries to calculate sum of the path from root to a given node in given Binary Tree | C # program for the above approach ; 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
using System ; using System . Collections . Generic ; 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 ; } Console . Write ( sum_of_node ) ; } public static void findSum ( List < int > Q ) { for ( int i = 0 ; i < Q . Count ; i ++ ) { int node_value = Q [ i ] ; sumOfNodeInAPath ( node_value ) ; Console . Write ( " ▁ " ) ; } } static public void Main ( ) { List < int > arr = new List < int > ( ) ; arr . Add ( 1 ) ; arr . Add ( 5 ) ; arr . Add ( 20 ) ; arr . Add ( 100 ) ; findSum ( arr ) ; } }