-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmax_path_sum.cpp
More file actions
25 lines (23 loc) · 908 Bytes
/
max_path_sum.cpp
File metadata and controls
25 lines (23 loc) · 908 Bytes
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
#include <bits/stdc++.h>
#include <string>
#include <vector>
#include <map>
#include <cmath>
/**
* Definition for a binary tree node.
* struct Node {
* int value;
* Node *left;
* Node *right;
* Node(int val, Node *left, Node *right) : value(val), left(left), right(right) {}
* };
*/
int max_sum = INT_MIN; // setting it to minimum possible integer
int maxPathSum(Node* root) {
if(root == NULL) return 0; // base case
int leftpath_max = max(maxPathSum(root->left), 0); // check whether sum along this path is positive
int rightpath_max = max(maxPathSum(root->right), 0); // if it isn't positive then don't consider this into path
int new_total = root->value + leftpath_max + rightpath_max; // consider sum along the path including root
max_sum = max(max_sum, new_total);
return root->value + max(leftpath_max, rightpath_max);
}