Day 19: (Binary Tree)

Maximum path sum Partially Solved on: Aug 5th 2020

int helper(TreeNode* root, int res) {
    if (root==NULL) {
        return 0;
    }
    int ls = helper(root->left, res);
    int rs = helper(root->right, res);
    int max_single = max(max(ls, rs) + root->val, root->val);
    
    int max_top = max(max_single, ls+rs+root->val);
    
    res = max(max_single, max_top);
    
    return res;
}

int maxPathSum(TreeNode* root) {
    int res=INT_MIN;
    int ans = helper(root, res);
    
    return ans;
}    

Symmetric Tree Solved on Aug 2nd 2020

Last updated

Was this helpful?