forked from Algo-Phantoms/Algo-Tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
max_tree_path.cpp
79 lines (67 loc) · 2.07 KB
/
max_tree_path.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*Given a binary tree in which each node element contains a number. Find the maximum possible sum from one leaf node to another.
The maximum sum path may or may not go through root.
For example, in the following binary tree, the maximum sum is 27(3 + 6 + 9 + 0 – 1 + 10). Expected time complexity is O(n)*/
#include<bits/stdc++.h>
using namespace std;
// Data structure to store a binary tree node
struct Node
{
int data;
Node *left, *right;
Node(int data)
{
this->data = data;
this->left = this->right = nullptr;
}
};
// Recursive function to find the maximum path sum "starting" from the
// given node in a binary tree. The maximum path sum between two nodes
// in a binary tree is updated in the reference variable `result`.
int findMaxPathSum(Node* node, int &result)
{
// base case: empty tree
if (node == nullptr) {
return 0;
}
// find maximum path sum "starting" from the left child
int left = findMaxPathSum(node->left, result);
// find maximum path sum "starting" from the right child
int right = findMaxPathSum(node->right, result);
// Try all possible combinations to get the optimal result
result = max(result, node->data);
result = max(result, node->data + left);
result = max(result, node->data + right);
result = max(result, node->data + left + right);
// return the maximum path sum "starting" from the given node
return max(node->data, node->data + max(left, right));
}
int main()
{
/* Construct the following tree
1
/ \
/ \
2 10
/ \ / \
-1 -4 -5 -6
/ / \
3 7 4
\
-2
*/
Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(10);
root->left->left = new Node(-1);
root->left->right = new Node(-4);
root->right->left = new Node(-5);
root->right->right = new Node(-6);
root->left->right->left = new Node(4);
root->right->left->left = new Node(7);
root->right->left->right = new Node(4);
root->right->left->left->right = new Node(-2);
int result = numeric_limits<int>::min();
findMaxPathSum(root, result);
cout << "The maximum path sum is " << result << endl;
return 0;
}