leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0337.cpp (433B)


      1 class Solution {
      2   public:
      3     int rob(TreeNode *root) const {
      4         if (!root) return 0;
      5         if (root->val < 0) return -root->val;
      6 
      7         int res = root->val;
      8         if (root->left) res += rob(root->left->left) + rob(root->left->right);
      9         if (root->right) res += rob(root->right->left) + rob(root->right->right);
     10 
     11         root->val = -max(res, rob(root->left) + rob(root->right));
     12         return -root->val;
     13     }
     14 };