Share my simple solution


  • 0
    O
    class Solution {
    private:
        pair<int, int> cookRob(TreeNode* root) {
            if (!root)
                return {0, 0};
            pair<int, int> left = cookRob(root->left);
            pair<int, int> right = cookRob(root->right);
            return {max(root->val + left.second + right.second, left.first + right.first), left.first + right.first};
        }
    public:
        int rob(TreeNode* root) {
            pair<int, int> ans = cookRob(root);
            return ans.first;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.