leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0337.cpp (433B)
0 class Solution {
1 public:
2 int rob(TreeNode *root) const {
3 if (!root) return 0;
4 if (root->val < 0) return -root->val;
6 int res = root->val;
7 if (root->left) res += rob(root->left->left) + rob(root->left->right);
8 if (root->right) res += rob(root->right->left) + rob(root->right->right);
10 root->val = -max(res, rob(root->left) + rob(root->right));
11 return -root->val;
12 }
13 };