<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 dfs(bool flag,TreeNode* node){
//         if(!node)return 0;
//         int pick=0;
//         if(flag){
        
//          pick = node->val + dfs(false, node->left) + dfs(false, node->right);
//         }
//        int not_pick = dfs(true, node->left) + dfs(true, node->right);
//         return max(pick,not_pick);
//     }
//     int rob(TreeNode* root) {
//         bool flag=true;
//         return dfs(flag,root);
//     }
// };


class Solution {
public:
    unordered_map<TreeNode*, int> node_to_index; // Map to store index of each node
    vector<vector<int>> memo; // 2D vector to store results, memo[node_index][flag]
    
    // Function to assign a unique index to each node
    int assignIndices(TreeNode* root, int idx) {
        if (!root) return idx;
        node_to_index[root] = idx; // Assign unique index to this node
        idx = assignIndices(root->left, idx + 1);
        return assignIndices(root->right, idx);
    }

    int dfs(bool flag, TreeNode* node){
        if (!node) return 0;
        
        // Get the index of the current node
        int node_idx = node_to_index[node];

        // Memoization: check if the result is already computed
        if (memo[node_idx][flag] != -1) return memo[node_idx][flag];
        
        int pick = 0;
        // If we are allowed to pick this node
        if (flag) {
            pick = node->val + dfs(false, node->left) + dfs(false, node->right);
        }

        // We can either pick or not pick this node
        int not_pick = dfs(true, node->left) + dfs(true, node->right);

        // Store the result in memo before returning
        return memo[node_idx][flag] = max(pick, not_pick);
    }

    int rob(TreeNode* root) {
        if (!root) return 0;
        
        // Assign unique indices to each node starting from 0
        int total_nodes = assignIndices(root, 0);

        // Initialize memo vector with size total_nodes x 2 (for flag = true/false)
        memo = vector<vector<int>>(total_nodes, vector<int>(2, -1));

        return dfs(true, root);
    }
};

25879