Sum of the Longest Bloodline of a Tree (Sum of nodes on the longest path from root to leaf node) | Practice | GeeksforGeeks

Given a binary tree of size N. Your task is to complete the function sumOfLongRootToLeafPath(), that find the sum of all nodes on the longest path from root to leaf node.
If two or more paths compete for the longest path, then the path having maximum sum of nodes is being considered.
Example 1:
Input: 
        4        
       / \       
      2   5      
     / \ / \     
    7  1 2  3    
      /
     6
Output: 13
Explanation:
        4        
       / \       
      2   5      
     / \ / \     
    7  1 2  3 
      /
     6

The highlighted nodes (4, 2, 1, 6) above are 
part of the longest root to leaf path having
sum = (4 + 2 + 1 + 6) = 13
Example 2:
Input: 
          1
        /   \
       2     3
      / \   / \
     4   5 6   7
Output: 11

Your Task:
You don't need to read input or print anything. Your task is to complete the function sumOfLongRootToLeafPath() which takes root node of the tree as input parameter and returns an integer denoting the sum of the longest root to leaf path of the tree. If the tree is empty, return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 <= Number of nodes <= 104
1 <= Data of a node <= 104
0
abhisoni1313 hours ago
     void  solve(Node* root , pair<int,int> sum , pair<int,int> &ans)   {       if(root == NULL )       {           return ;        }
              int curr_data = root->data ;               sum.first = sum.first + curr_data ;       sum.second = sum.second + 1  ;              if(root->left == NULL && root->right == NULL)       {           if(ans.second <= sum.second )           {               ans = make_pair(sum.first , sum.second) ;           }       }              solve(root->left , sum , ans) ;       solve(root->right , sum , ans ) ;                 }      int sumOfLongRootToLeafPath(Node *root)   {       //code here       pair<int,int> ans ;       ans = make_pair(0,0) ;              pair<int,int> sum ;       sum = make_pair(0,0) ;              solve(root,sum, ans) ;              return ans.first ;          }
0
aishapervin03
This comment was deleted.
0
tirtha19025684 days ago
class Solution{
       int sum = 0;
       int len = 0;
   public int sumOfLongRootToLeafPath(Node root)
   {
       if(root == null) return 0;
       int currSum = 0;
       int currLen = 0;
       solve(root,currSum,currLen);
       return sum;
   }
   
   void solve(Node root,int currSum,int currLen){
       
       // leaf is reached 
       if(root == null){
           if(currLen > len){
               len = currLen;
               sum = currSum;
           }
           else if(currLen == len){
               sum = Math.max(sum,currSum);
           }
           return;
       }
       
       currSum = currSum + root.data;
       
       solve(root.left,currSum,currLen+1);
       solve(root.right,currSum,currLen+1);
       
   }
   
0
guptatushar110411 week ago
vector<int>solve(Node*root)   {       if(root==NULL)       return{0,0};       vector<int>a=solve(root->left);       vector<int>b=solve(root->right);              if(a[0]>b[0])       return {a[0]+1,a[1]+root->data};              if(a[0]<b[0])       return {b[0]+1,b[1]+root->data};              else       return {a[0]+1,max(a[1],b[1])+root->data};   }   int sumOfLongRootToLeafPath(Node *root)   {       //code here       vector<int>ans=solve(root);       return ans[1];   }
0
sourabhsuryawanshitigerindia3 weeks ago
C++ || Easy solution || Recursion method 
class Solution{public:   void solve(Node* root, int len, int &maxlen, int sum, int &maxsum){       // base case       if(root == NULL){           if(len > maxlen){               maxlen = len;               maxsum =sum;           }           else if(len == maxlen){               maxsum = max(maxsum,  sum);           }           return;       }              sum = sum + root -> data;              // recursive call       solve(root -> left ,len+ 1, maxlen, sum, maxsum);       solve(root ->right ,len+ 1, maxlen, sum, maxsum);      }   int sumOfLongRootToLeafPath(Node *root)   {       int len = 0;       int maxlen = 0;       int sum = 0;       int maxsum =0;              // call function       solve(root, len, maxlen,sum, maxsum);       return maxsum;          }};
0
ruchitchudasama1231 month ago
class Solution
{
public:

void helper(Node* root,int &maxi,int &len,int sum,int level){
    
    if(!root){
        if(len<=level){
            len=max(level,len);
            maxi=max(maxi,sum);
        }
        return;
    }
    sum+=root->data;    
    // cout<<sum<<" "<<root->data<<endl;
    // cout<<level<<" "<<len<<endl;
    helper(root->left,maxi,len,sum,level+1);
    helper(root->right,maxi,len,sum,level+1);
    
}
    
    int sumOfLongRootToLeafPath(Node *root)
    {
        int maxi=INT_MIN,len=0;
        int sum=0,level=0;
        helper(root,maxi,len,sum,level);
        return maxi;
    }
};
0
2019kucp10851 month ago
//easy cpp code, 0.3s
void helper(Node* root,int len,int sum, int &ml, int &ms)
    {
        if(root==NULL) return;
        
        if(root->left==NULL && root->right==NULL && sum+root->data>=ms && ml<=len)
        {
            ms= root->data+sum;
            ml= len;
            return;
        }
        helper(root->left,len+1,root->data+sum,ml,ms);
        helper(root->right,len+1,root->data+sum,ml,ms);
    }
    
    int sumOfLongRootToLeafPath(Node *root)
    {
         int ml= INT_MIN;
         int ms=0;
         helper(root,0,0,ml,ms);
         return ms;
    }
+2
sfazal0001 month ago
int maxSum;int maxLevel;
int  solve(node* root ,int level,int sum){   if(root == NULL)   {       return 0;   }      sum = sum + root->data;   level = level +1;      solve(root->left, level, sum);   solve(root->right , level,sum);      if(level> maxLevel)   {       maxLevel = level;       maxSum = sum;   }   else if(maxLevel == level)   {       maxSum = max(maxSum , sum);   }}
 
int MaxPathSum(node* root){   maxSum = INT_MIN;   maxLevel = INT_MIN;      int sum = 0;   int level = 0;      solve( root,level, sum);      return maxSum;}    
0
shrutibhargav402 months ago
class Solution{private:   void solve(Node* root, int len, int& maxlen, int sum, int& maxsum){       if(root==NULL){           if(len > maxlen){               maxlen = len;               maxsum = max(sum, maxsum);           }           else if(len == maxlen){               maxsum = max(sum, maxsum);           }           return;       }       sum = sum + root->data;       solve(root->left, len+1, maxlen, sum, maxsum);       solve(root->right, len+1, maxlen, sum, maxsum);          }    public:      int sumOfLongRootToLeafPath(Node *root)   {       //code here       int len = 0;       int maxlen = 0;       int sum = 0;       int maxsum = INT_MIN;       solve(root, len, maxlen, sum, maxsum);       return maxsum;   }};
0
vrajeshmodi992 months ago
 void helper(Node*root,vector<pair<int,int>>&v,int level,int sum){
        
        if(!root){
            return;
        }
        if(!root->left && !root->right){
            pair<int,int>p;
            p = {level,sum+root->data};
            v.push_back(p);
            return;
        }
        if(root->left){
            helper(root->left,v,level+1,sum+root->data);
        }
        if(root->right){
            helper(root->right,v,level+1,sum+root->data);
        }        
        
    }
    
    
    int sumOfLongRootToLeafPath(Node *root)
    {
        vector<pair<int,int>>v;
        helper(root,v,1,0);
        
        sort(v.begin(),v.end());
        int n = v.size();
        int ans = v[n-1].second;
        
        return ans;
        
    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.