<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p>

<p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p>

<p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" />
<pre>
<strong>Input:</strong> root = [3,2,3,null,3,null,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" />
<pre>
<strong>Input:</strong> root = [3,4,5,1,3,null,1]
<strong>Output:</strong> 9
<strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li>
	<li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li>
</ul>

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int solve( TreeNode * root , bool can)
    {
        if( root == NULL )
        {
            return 0;
        }
        int ans = 0;
        if( can )
        {
            // include 
            int in = root->val;
            in += solve( root->left , false);
            in += solve( root->right , false );

            // exclude 
            int ex = 0;
            ex += solve( root->left , true );
            ex += solve( root->right , true );

            ans = max( in , ex );

        }
        else
        {
            ans += solve( root->left , true );
            ans += solve( root->right , true );
        }
        return ans;
    }
        struct pair_hash {
    template <class T1, class T2>
    std::size_t operator()(const std::pair<T1, T2>& p) const {
        auto hash1 = std::hash<T1>{}(p.first);
        auto hash2 = std::hash<T2>{}(p.second);
        return hash1 ^ hash2;
    }
};
    // non continuous DP
    int solveUsingMem( TreeNode * root , bool can , unordered_map<std::pair<TreeNode*, bool>, int, pair_hash> &dp )
    {
        if( root == NULL )
        {
            return 0;
        }
        if( dp.find({root , can }) != dp.end())
        {
            return dp[{ root , can }];
        }
        int ans = 0;
        if( can )
        {
            // include 
            int in = root->val;
            in += solveUsingMem( root->left , false, dp);
            in += solveUsingMem( root->right , false , dp);

            // exclude 
            int ex = 0;
            ex += solveUsingMem( root->left , true ,dp);
            ex += solveUsingMem( root->right , true , dp);

            ans = max( in , ex );

        }
        else
        {
            ans += solveUsingMem( root->left , true , dp );
            ans += solveUsingMem( root->right , true , dp);
        }
        dp[{root , can }]  = ans;
        return dp[{root , can }];
    }


// Define the unordered_map with the custom hash function
// std::unordered_map<std::pair<TreeNode*, bool>, int, pair_hash> dp;
    int rob(TreeNode* root) {
        // unordered_map< pair<TreeNode * , bool >, int > dp;
        unordered_map<std::pair<TreeNode*, bool>, int, pair_hash> dp;
        return solveUsingMem( root , true , dp);
    }
};
29681