-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMaximum-Path-Sum-Between-Two-Leaves.cpp
59 lines (52 loc) · 1.71 KB
/
Maximum-Path-Sum-Between-Two-Leaves.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <bits/stdc++.h>
using namespace std;
template <typename T>
class TreeNode
{
public :
T val;
TreeNode<T> *left;
TreeNode<T> *right;
TreeNode(T val)
{
this -> val = val;
left = NULL;
right = NULL;
}
};
// Helper function to find the maximum sum between two leaves
// and the maximum sum from root to any leaf of current subtree
pair<long long, long long> solve(TreeNode<int> *root) {
// if root is null, return {-1, -1}
if(!root) {
return {-1, -1};
}
// if root is a leaf node, return {-1, root->val}
if(!root->left && !root->right) {
return {-1, root->val};
}
// find the maximum sum between two leaves in left subtree
pair<long long, long long> left = solve(root->left);
// find the maximum sum between two leaves in right subtree
pair<long long, long long> right = solve(root->right);
// if left subtree is null, return {-1, right.second + root->val}
if(!root->left) {
return {-1, right.second + root->val};
}
// if right subtree is null, return {-1, left.second + root->val}
if(!root->right) {
return {-1, left.second + root->val};
}
// find the maximum sum between two leaves in current subtree
long long curr = left.second + right.second + root->val;
long long first = max(left.first, max(right.first, curr));
// find the maximum sum from root to any leaf of current subtree
long long second = max(left.second, right.second) + root->val;
// return {first, second}
return {first, second};
}
long long int findMaxSumPath(TreeNode<int> *root)
{
// Call the helper function and return the first element of the pair
return solve(root).first;
}