1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
class Solution { public: int maxPathSum(TreeNode* root) { int res = INT_MIN; helper(root, res); return res; }
int helper(TreeNode *root, int &res) { if (!root) return 0; int l = max(0, helper(root->left, res)); int r = max(0, helper(root->right, res)); res = max(res, root->val + l + r); return root->val + max(l, r); } };
|