标签:htm pre www bin pat div com root span
题目:
Given a binary tree, find the maximum path sum.
The path may start and end at any node in the tree.
Example
Given the below binary tree:
1
/ 2 3
return 6
.
题解:
Solution 1 () from here
class Solution { public: int maxPathSum(TreeNode *root) { if (root == NULL) { return 0; } int res = INT_MIN; helper(root, res); return res; } int helper(TreeNode *root, int &res) { if (root == NULL) { return 0; } int sum = root->val; int leftMax = helper(root->left, res); int rightMax = helper(root->right, res); if (leftMax > 0) { sum += leftMax; } if (rightMax > 0) { sum += rightMax; } res = max(sum, res); return max(0, max(leftMax, rightMax)) + root->val; } };
【Lintcode】094.Binary Tree Maximum Path Sum
标签:htm pre www bin pat div com root span
原文地址:http://www.cnblogs.com/Atanisi/p/6832653.html