• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
March 21, 2024 |870 Views
PROBLEM OF THE DAY : 20/03/2024 | Sum of nodes on the longest path from root to leaf node
Description
Discussion

Welcome to the daily solving of our PROBLEM OF THE DAY with Yash Dwivedi. We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Tree but also build up problem-solving skills.

In this problem, we are given a binary tree having n nodes. Find the sum of all nodes on the longest path from root to any leaf node. If two or more paths compete for the longest path, then the path having maximum sum of nodes will be considered.

Example :

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

Give the problem a try before going through the video. All the best!!!
Problem Link: https://www.geeksforgeeks.org/problems/sum-of-the-longest-bloodline-of-a-tree/1