NC6 二叉树的最大路径和
class Solution {
public:
int Max=INT32_MIN;
int build(TreeNode *root)
{
if(root==NULL ) return 0;
if(root->right==NULL&&root->left==NULL)
{
Max=max(Max,root->val);
return root->val;
}
int left=build(root->left) ;
int right=build(root->right);
left=max(left,0);
right=max(right,0);
Max=max(Max,left+right+root->val);
return max(left,right)+root->val;
}
int maxPathSum(TreeNode* root) {
// write code here
build(root);
return Max;
}
};