class Solution {
public:
int cal_sum(TreeNode* root,int& ans){
if(!root)return 0;
int left_sum=max(0,cal_sum(root->left, ans));
int right_sum=max(0,cal_sum(root->right, ans));
int temp_sum=root->val+left_sum+right_sum;
ans=max(ans,temp_sum);
return max(left_sum,right_sum)+root->val;
}
int maxPathSum(TreeNode* root) {
if(!root)return 0;
int ans=INT_MIN;
cal_sum(root, ans);
return ans;
}
};
124. Binary Tree Maximum Path Sum
最新推荐文章于 2022-06-17 14:44:33 发布