Skip to content
Related Articles

Related Articles

Queries to calculate sum of the path from root to a given node in given Binary Tree
  • Difficulty Level : Easy
  • Last Updated : 12 Mar, 2021

Given an infinite complete binary tree rooted at node 1, where every ith node has two children, with values 2 * i and 2 * (i + 1). Given another array arr[] consisting of N positive integers, the task for each array element arr[i] is to find the sum of the node values that occur in a path from the root node to the node arr[i].

Examples:

Input: arr[] = {3, 10}
Output: 4 18
Explanation:
Node 3: The path is 3 -> 1. Therefore, the sum of the path is 4.
Node 10: The path is 10 -> 5 -> 2 -> 1. Therefore, the sum of node is 18.

Input: arr[] = {1, 4, 20}
Output: 1 7 38
Explanation:
Node 1: The path is 1. Therefore, the sum of the path is 1.
Node 4: The path is 4 -> 2 -> 1. Therefore, the sum of node is 7.
Node 20: The path is 20 -> 10 -> 5 -> 2 -> 1. Therefore, the sum of node is 38.

Naive Approach: The simplest approach is to perform DFS Traversal for each array element arr[i] to find its path from the current node to the root and print the sum of the node values in that path. 
Time Complexity: O(N * H), where H is the maximum height of the tree.
Auxiliary Space: O(H)

Efficient Approach: The above approach can also be optimized based on the observation that the parent of the node with value N contains the value N/2. Follow the steps below to solve the problem:



  • Initialize a variable, say sumOfNode, to store the sum of nodes in a path.
  • Traverse the array arr[i] and perform the following steps:
    • For each element arr[i], update the value of sumOfNode as sumOfNode + X and update arr[i] as arr[i] / 2.
    • Repeat the above steps while arr[i] is greater than 0.
    • Print the value of sumOfNode as the result for each array element arr[i].

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <iostream>
#include <vector>
using namespace std;
 
// Function to find the sum of the
// path from root to the current node
void sumOfNodeInAPath(int node_value)
{
    // Sum of nodes in the path
    int sum_of_node = 0;
 
    // Iterate until root is reached
    while (node_value) {
 
        sum_of_node += node_value;
 
        // Update the node value
        node_value /= 2;
    }
 
    // Print the resultant sum
    cout << sum_of_node;
 
    return;
}
 
// Function to print the path
// sum for each query
void findSum(vector<int> Q)
{
    // Traverse the queries
    for (int i = 0; i < Q.size(); i++) {
 
        int node_value = Q[i];
 
        sumOfNodeInAPath(node_value);
 
        cout << " ";
    }
}
 
// Driver Code
int main()
{
    vector<int> arr = { 1, 5, 20, 100 };
    findSum(arr);
 
    return 0;
}

Java




/*package whatever //do not write package name here */
 
import java.io.*;
import java.util.ArrayList;
class GFG {
 
  // Function to find the sum of the
  // path from root to the current node
  public static void sumOfNodeInAPath(int node_value)
  {
    // Sum of nodes in the path
    int sum_of_node = 0;
 
    // Iterate until root is reached
    while (node_value > 0) {
 
      sum_of_node += node_value;
 
      // Update the node value
      node_value /= 2;
    }
 
    // Print the resultant sum
    System.out.print(sum_of_node);
  }
 
  // Function to print the path
  // sum for each query
  public static void findSum(ArrayList<Integer> Q)
  {
    // Traverse the queries
    for (int i = 0; i < Q.size(); i++) {
 
      int node_value = Q.get(i);
 
      sumOfNodeInAPath(node_value);
 
      System.out.print(" ");
    }
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    // arraylist to store integers
    ArrayList<Integer> arr = new ArrayList<>();
    arr.add(1);
    arr.add(5);
    arr.add(20);
    arr.add(100);
    findSum(arr);
  }
}
 
// This code is contributed by aditya7409.

Python3




# Python program for the above approach
 
# Function to find the sum of the
# path from root to the current node
def sumOfNodeInAPath(node_value):
     
    # Sum of nodes in the path
    sum_of_node = 0
     
    # Iterate until root is reached
    while (node_value):
         
        sum_of_node += node_value
         
        # Update the node value
        node_value //= 2
         
    # Prthe resultant sum
    print(sum_of_node, end = " ")
     
# Function to prthe path
# sum for each query
def findSum(Q):
     
    # Traverse the queries
    for i in range(len(Q)):
        node_value = Q[i]
        sumOfNodeInAPath(node_value)
        print(end = "")
 
# Driver Code
arr = [1, 5, 20, 100]
findSum(arr)
 
# This code is contributed by shubhamsingh10

C#




// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
  // Function to find the sum of the
  // path from root to the current node
  public static void sumOfNodeInAPath(int node_value)
  {
     
    // Sum of nodes in the path
    int sum_of_node = 0;
 
    // Iterate until root is reached
    while (node_value > 0) {
 
      sum_of_node += node_value;
 
      // Update the node value
      node_value /= 2;
    }
 
    // Print the resultant sum
    Console.Write(sum_of_node);
  }
 
  // Function to print the path
  // sum for each query
  public static void findSum(List<int> Q)
  {
    // Traverse the queries
    for (int i = 0; i < Q.Count ; i++) {
 
      int node_value = Q[i];
      sumOfNodeInAPath(node_value);
      Console.Write(" ");
    }
  }
 
// Driver Code
static public void Main()
{
   
    // arraylist to store integers
    List<int> arr = new List<int>();
    arr.Add(1);
    arr.Add(5);
    arr.Add(20);
    arr.Add(100);
    findSum(arr);
}
}
// This code is contributed by sanjoy_62.
Output: 
1 8 38 197

 

Time Complexity: O(N*log X), where X is the maximum element of the array.
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :