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;
}
};
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;
}
};
相关文章推荐
- Binary Tree Maximum Path Sum--LeetCode
- [leetcode]Binary Tree Maximum Path Sum
- [leetcode] Binary Tree Maximum Path Sum
- LeetCode Binary Tree Maximum Path Sum
- 二叉树最大路径和-Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- 124. Binary Tree Maximum Path Sum
- binary-tree-maximum-path-sum(二叉树的最大路径和)
- [LeetCode]Binary Tree Maximum Path Sum
- [LeetCode] Binary Tree Maximum Path Sum
- 124. Binary Tree Maximum Path Sum Leetcode Python
- [Leetcode][JAVA] Binary Tree Maximum Path Sum
- LeetCode OJ-- Binary Tree Maximum Path Sum ***
- leetcode--Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Leetcode 124 Binary Tree Maximum Path Sum
- 5.4.5 Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum