您的位置:首页 > 其它

binary-tree-maximum-path-sum

2018-03-02 22:30 555 查看
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

Return6.class Solution {
public:
int maxPathSum(TreeNode *root) {
if(root==NULL)
return 0;
if((root->right==NULL)&&(root->left==NULL)){
return Max=root->val;
}
else{
__maxPathSum(root);
}
return Max;

}
private:
int Max=INT_MIN;
int __maxPathSum(TreeNode *root) {
if(root==NULL)
return 0;
int left=max(0,__maxPathSum(root->left));
int right=max(0,__maxPathSum(root->right));
int re=max(left,right)+root->val;
Max=max(Max,left+right+root->val);
return re;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: