-
Notifications
You must be signed in to change notification settings - Fork 160
/
Maximalpathsum.cpp
73 lines (54 loc) · 1.62 KB
/
Maximalpathsum.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
/*
Given a N-way tree, find the maximum path sum. The path may start and end at any node in the tree.
*/
/*
solution: calculate each path sum for each child, add it to maximal path if it is positve
O(n) time, O(1) space
*/
#include<iostream>
#include<vector>
using namespace std;
struct TreeNode {
int val;
vector<TreeNode*> children;
TreeNode(int n) : val(n), children() {}
};
int MaxPathSumInner(TreeNode *node, int &maxSoFar) {
if (node == NULL) return 0;
int maxPath = node->val;
int tempPath = INT_MIN;
vector<TreeNode*>::iterator it = node->children.begin();
for (; it != node->children.end(); it++) {
int maxchild = MaxPathSumInner(*it, maxSoFar);
if (maxchild > 0) maxPath += maxchild;
if (maxchild > tempPath) tempPath = maxchild;
}
if (maxPath > maxSoFar) maxSoFar = maxPath;
int res = node->val;
return max(res, res + tempPath); //note path definition
}
int MaxPathSum(TreeNode *root) {
if (root == NULL) return 0;
int maxSoFar = root->val;
MaxPathSumInner(root, maxSoFar);
return maxSoFar;
}
int main() {
TreeNode *root = new TreeNode(-10);
TreeNode *p1 = new TreeNode(2);
TreeNode *p2 = new TreeNode(3);
TreeNode *p3 = new TreeNode(4);
root->children.push_back(p1);
root->children.push_back(p2);
root->children.push_back(p3);
TreeNode *p4 = new TreeNode(5);
TreeNode *p5 = new TreeNode(-1);
p2->children.push_back(p4);
p2->children.push_back(p5);
TreeNode *p6 = new TreeNode(6);
p5->children.push_back(p6);
TreeNode *p7 = new TreeNode(-1);
p6->children.push_back(p7);
cout<<MaxPathSum(root);
return 0;
}