29. Sum of nodes on the longest path
GFG solution to the Sum of Nodes on the Longest Path problem using DFS.
The problem can be found at the following link: π Question Link
π§© Problem Description
Given a binary tree, find the sum of the nodes on the longest path from the root to any leaf.
If there are multiple root-to-leaf paths of the same maximum length, return the maximum sum among them.
π Examples
Example 1
Input: root = [4,2,5,7,1,2,3,null,null,6]
Output: 13
Explanation:
Longest path is 4 β 2 β 1 β 6, sum = 4+2+1+6 = 13
Example 2
Input: root = [1,2,3,4,5,6,7]
Output: 11
Explanation:
Longest path is 1 β 3 β 7, sum = 1+3+7 = 11
Example 3
Input: root = [10,5,15,3,7,null,20,1]
Output: 19
Explanation:
Longest path is 10 β 5 β 3 β 1, sum = 10+5+3+1 = 19
π Constraints
Number of nodes in the tree: $1 \le N \le 10^6$
Node values: $0 \le \text{data} \le 10^4$
β
My Approach
DFS Returning (Length, Sum)
Idea:
For each subtree, compute a pair
(maxDepth, maxSum)
, wheremaxDepth
is the maximum root-to-leaf depth, andmaxSum
is the maximum sum along any path of that depth.
Recurrence:
If left depth > right depth: take
(left.depth+1, left.sum + node->data)
.If right depth > left depth: take
(right.depth+1, right.sum + node->data)
.If equal: take
(left.depth+1, max(left.sum, right.sum) + node->data)
.
Answer:
After processing the root, its
second
component is the desired sum.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(N), as we visit each node exactly once.
Expected Auxiliary Space Complexity: O(H), where H = height of the tree (recursion stack).
π§βπ» Code (C++)
class Solution {
public:
pair<int, int> dfs(Node* root) {
if (!root) return {0, 0};
auto l = dfs(root->left), r = dfs(root->right);
if (l.first > r.first) return {l.first + 1, l.second + root->data};
if (r.first > l.first) return {r.first + 1, r.second + root->data};
return {l.first + 1, max(l.second, r.second) + root->data};
}
int sumOfLongRootToLeafPath(Node* root) {
return dfs(root).second;
}
};
π§βπ» Code (Java)
class Solution {
public int sumOfLongRootToLeafPath(Node root) {
return dfs(root)[1];
}
int[] dfs(Node node) {
if (node == null) return new int[]{0, 0};
int[] l = dfs(node.left), r = dfs(node.right);
if (l[0] > r[0]) return new int[]{l[0] + 1, l[1] + node.data};
if (r[0] > l[0]) return new int[]{r[0] + 1, r[1] + node.data};
return new int[]{l[0] + 1, Math.max(l[1], r[1]) + node.data};
}
}
π Code (Python)
class Solution:
def sumOfLongRootToLeafPath(self, root):
def dfs(node):
if not node: return (0, 0)
l = dfs(node.left)
r = dfs(node.right)
if l[0] > r[0]: return (l[0] + 1, l[1] + node.data)
if r[0] > l[0]: return (r[0] + 1, r[1] + node.data)
return (l[0] + 1, max(l[1], r[1]) + node.data)
return dfs(root)[1]
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated