class Solution { int maxx=INT_MIN; public: 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; } };
Preview:
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