-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
43 lines (36 loc) · 1.13 KB
/
Copy pathsolution.cpp
File metadata and controls
43 lines (36 loc) · 1.13 KB
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
/*
class Node {
public:
int data;
Node *left;
Node *right;
Node(int val) {
data = val;
left = right = NULL;
}
};
*/
#include <climits>
#include <algorithm>
class Solution {
private:
// helper returns maximum downward path sum for a node (to be used by parent)
int helper(Node* node, int &globalMax) {
if (!node) return 0;
// compute max contribution from left and right; ignore negatives
int left = std::max(0, helper(node->left, globalMax));
int right = std::max(0, helper(node->right, globalMax));
// best path that uses node as highest point (could be global best)
int current = node->data + left + right;
globalMax = std::max(globalMax, current);
// return the best single-sided path to connect with parent
return node->data + std::max(left, right);
}
public:
int findMaxSum(Node *root) {
if (!root) return 0; // if empty tree (problem constraints usually have >=1 node)
int globalMax = INT_MIN;
helper(root, globalMax);
return globalMax;
}
};