-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq_124_max_tree_path_sum.cpp
More file actions
35 lines (32 loc) · 1.07 KB
/
q_124_max_tree_path_sum.cpp
File metadata and controls
35 lines (32 loc) · 1.07 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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
// Learned from
// https://leetcode.com/problems/binary-tree-maximum-path-sum/solutions/603423/python-recursion-stack-thinking-process-diagram/?envType=study-plan-v2&envId=top-interview-150
int max_path;
int maxPathSum(TreeNode* root) {
max_path = root->val;
max_gain(root);
return max_path;
}
int max_gain(TreeNode *root) {
if (root == nullptr) {
return 0;
}
int left_gain = 0, right_gain = 0;
left_gain = max(max_gain(root->left), 0);
right_gain = max(max_gain(root->right), 0);
max_path = max(max_path, root->val + left_gain + right_gain);
return root->val + max(left_gain, right_gain);
}
};