https://www.cnblogs.com/grandyang/p/4280120.html
分清楚一个东西,你如果需要左子树或者右子树加入路径,不会让两个一起,只会让一个,只有真正是最大的那个才会同时使用左右子树
class Solution { public: int maxPathSum(TreeNode* root) { int res = INT_MIN; maxpath(root,res); return res; } int maxpath(TreeNode* root,int &res){ if(root == NULL) return 0; int left = max(0,maxpath(root->left,res)); int right = max(0,maxpath(root->right,res)); res = max(res,left + right + root->val); return max(left,right) + root->val; } };
124. Binary Tree Maximum Path Sum
原文:https://www.cnblogs.com/ymjyqsx/p/10524898.html