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
Return 6.
水题。可以加剪枝做到稍稍优化。
class Solution {
public:
int max;
int maxPathSum(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
max = -9999999;
getMax(root);
return max;
}
int getMax(TreeNode *root)
{
int c1 = root->val;
int c2 = c1;
int temp1 = 0;
int temp2 = 0;
if(root->left != NULL)
{
temp1 = getMax(root->left);
if(temp1 < 0)
temp1 = 0;
}
if(root->right != NULL)
{
temp2 = getMax(root->right);
if(temp2 < 0)
temp2 = 0;
}
c1 += (temp1 >= temp2)?temp1:temp2;
c2 += temp1 + temp2;
if(c2 > max)
max = c2;
return c1;
}
};