Given a binary tree, find the maximum path sum.
The path may start and end at any node in the tree.
For example:
Given the below binary tree,
1
/ \
2 3
*/void findMaxPath(Node<int>* curr, int & cSum, int & maxSum){
if(curr==NULL){
cSum = 0;
return;
}
int lsum = 0;
int rsum = 0;
findMaxPath(curr->Left, lsum, maxSum);
findMaxPath(curr->Right, rsum, maxSum);
cSum = max(curr->Value, max(curr->Value+lsum, curr->Value+rsum));
maxSum = max(maxSum, max(cSum, curr->Value+lsum+rsum));
}
No comments:
Post a Comment