Skip to content

Commit 24d78df

Browse files
authored
Create main.cpp
1 parent c189697 commit 24d78df

File tree

1 file changed

+33
-0
lines changed
  • 17 - Binary Tree Data Structure Problems/24 - Sum of Nodes on The Longest Path from Root to Leaf Node

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution
2+
{
3+
public:
4+
void solve(Node* root, int sum, int &maxSum, int len, int &maxLen){
5+
if(root == NULL){
6+
if(len > maxLen){
7+
maxLen = len;
8+
maxSum = sum;
9+
}else if(len == maxLen){
10+
maxSum = max(sum, maxSum);
11+
}
12+
return;
13+
}
14+
15+
sum = sum + root -> data;
16+
solve(root -> left, sum, maxSum, len+1, maxLen);
17+
solve(root -> right, sum, maxSum, len+1, maxLen);
18+
}
19+
20+
int sumOfLongRootToLeafPath(Node *root)
21+
{
22+
23+
int len = 0;
24+
int maxLen = 0;
25+
26+
int sum = 0;
27+
int maxSum = INT_MIN;
28+
29+
solve(root, sum, maxSum, len, maxLen);
30+
31+
return maxSum;
32+
}
33+
};

0 commit comments

Comments
 (0)