knakul853

Untitled

Jul 22nd, 2020
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.52 KB | None | 0 0
  1. /**
  2. knakul853
  3.  */
  4. class Solution {
  5. public:
  6.     int ans = INT_MIN;
  7.    
  8.     int solve(TreeNode* root)
  9.     {
  10.         if(!root)return 0;
  11.         int left = max(solve(root->left),0);
  12.         int right = max(solve(root->right),0);
  13.         ans = max(ans , left+right+root->val);
  14.         return max(left , right) + root->val;
  15.     }
  16.     int maxPathSum(TreeNode* root) {
  17.         if(!root)
  18.         {
  19.             return 0;
  20.         }
  21.        
  22.          solve(root);
  23.         return ans;
  24.        
  25.        
  26.        
  27.     }
  28. };
Add Comment
Please, Sign In to add comment