<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 rob(TreeNode* root) {
        if(root==NULL)
            return 0;
        vector<TreeNode*>arr;
        unordered_map<TreeNode*, int>mp;
        queue<TreeNode*>q;
        q.push(root);
        while(!q.empty())
        {
            int k = q.size();
            for(int i=0; i<k; i++)
            {
                TreeNode*temp = q.front();
                q.pop();
                if(temp)
                {
                    mp[temp] = arr.size();
                    arr.push_back(temp);
                }
                else
                {
                    arr.push_back(NULL);
                    continue;
                }
                
                q.push(temp->left);
                q.push(temp->right);
            }
        }
        // for(int i=0; i<arr.size(); i++)
        //     cout<<arr[i]->val<<" ";
        
        // n, 2*n+1, 2*n+2, 0/1  max(dp[0][0], dp[0][1])
        // dp[i][1] = i->val + max(dp[2*i+1][0], dp[2*i+2][1]);
        // dp[i][0] = max(dp[2*i+1][0], dp[2*i+1][1]) + max(dp[2*])
        int n = arr.size();
        vector<vector<int>>dp(n, vector<int>(2));
        for(int i=n-1; i>=0; i--)
        {
            if(arr[i]==NULL)
                continue;
            else
            {
                dp[i][1] = arr[i]->val;
                TreeNode*node = arr[i];
                if(node)
                {
                    if(node->left)
                        dp[i][1]+=dp[mp[node->left]][0];
                    if(node->right)
                        dp[i][1]+=dp[mp[node->right]][0];
                    if(node->left)
                        dp[i][0] = max(dp[mp[node->left]][0], dp[mp[node->left]][1]);
                    if(node->right)
                        dp[i][0]+=max(dp[mp[node->right]][0], dp[mp[node->right]][1]);
                }
            }
            cout<<endl;
            cout<<i<<": "<<dp[i][0]<<" "<<dp[i][1]<<endl;
        }
        return max(dp[0][0], dp[0][1]);
    }
};
25668