题解 | #二叉树中的最大路径和#
二叉树中的最大路径和
http://www.nowcoder.com/practice/da785ea0f64b442488c125b441a4ba4a
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
class Solution {
public:
/**
*
* @param root TreeNode类
* @return int整型
*/
int ans = INT_MIN;
int deal(TreeNode* root) {
if (!root) return 0;
int left = max(deal(root->left), 0);
int right = max(deal(root->right), 0);
ans = max(ans, root->val + left + right);
return max(left, right) + root->val;
}
int maxPathSum(TreeNode* root) {
// write code here
deal(root);
return ans;
}
};