
g0101_0200.s0124_binary_tree_maximum_path_sum.Solution.cpp Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Dynamic_Programming #Depth_First_Search
// #Tree #Binary_Tree #Udemy_Tree_Stack_Queue #Big_O_Time_O(N)_Space_O(N)
// #2024_05_27_Time_12_ms_(92.72%)_Space_26_MB_(75.74%)
#include
#include
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
private:
int maxSum;
int helper(TreeNode* root) {
if (root == nullptr) {
return 0;
}
// to avoid negative values on the left side, we compare them with 0
int left = std::max(0, helper(root->left));
int right = std::max(0, helper(root->right));
int current = root->val + left + right;
maxSum = std::max(maxSum, current);
return root->val + std::max(left, right);
}
public:
int maxPathSum(TreeNode* root) {
maxSum = INT_MIN;
helper(root);
return maxSum;
}
};
© 2015 - 2025 Weber Informatics LLC | Privacy Policy