-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximum_path_sum_of_BT.cpp
43 lines (37 loc) · 1.51 KB
/
maximum_path_sum_of_BT.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
/*
# Problem Statement: To find the sum of the maximum path of a BT (Asked in Amazon)
---------------------------------------------------------------
# How to think:
* If for every node, we can find the maximum path sum, we can generate the max sum
* For every node, the max path sum will be the value of the node + the max of the left and right sum being generated
// Got Stuck here
* While doing this process, we need to keep track of the maximum sum being generated at each node, i.e max(maxi, node->val + left + right)
* Note: If we are getting the sum(node->val + max(lh, rh)) to be negative, we will not take it, therefore, return 0
---------------------------------------------------------------
*/
#include<bits/stdc++.h>
using namespace std;
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:
int calculateMaxi(TreeNode* node, int& maxi) {
if(node == nullptr) return 0;
int lh = calculateMaxi(node->left, maxi);
int rh = calculateMaxi(node->right, maxi);
maxi = max(node->val + lh + rh, maxi);
if((node->val + max(lh, rh)) < 0) return 0;
return (node->val + max(lh, rh));
}
int maxPathSum(TreeNode* root) {
int maxi = root->val;
int val = calculateMaxi(root, maxi);
return maxi;
}
};