Advertisement
sweet1cris

Untitled

Oct 18th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.45 KB | None | 0 0
  1.   int helper(TreeNode* root, int& max) {
  2.     if (root == NULL) {
  3.       return 0;
  4.     }
  5.  
  6.     int left = helper(root->left, max);
  7.     int right = helper(root->right, max);    // step 1
  8.  
  9.     left = left < 0  0 : left;      // step 2
  10.     right = right < 0  0 : right;
  11.     max = max > (root->value + left + right)  max : (root->value + left + right);
  12.  
  13.     return left > right  (left + root->value) : (right + root->value);  // step 3
  14.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement