class Solution {
    int maxx=INT_MIN;
    int ans(TreeNode* root)
        if(root==NULL)return 0;
        int l = ans(root->left);
        int r = ans(root->right);
        maxx = max(maxx,l+r+root->val);
        return max(0,max(l+root->val,r+root->val));
    int maxPathSum(TreeNode* root) {
        int pathSum = ans(root);
        return maxx;
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter