<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:
    unordered_map<TreeNode*, int> mp;

    int helper(TreeNode* root){
        if(root == nullptr)return(0);
        if(root->left == nullptr && root->right == nullptr){mp[root]=root->val;return(root->val);}
        if(mp.find(root)!=mp.end())return(mp[root]);

        int ans = 0;
        int x,y;
        if(root->left != nullptr){x = helper(root->left->left) + helper(root->left->right);}
        else x = 0;
        if(root->right != nullptr){y = helper(root->right->left) + helper(root->right->right);}
        else y = 0;

        ans = x+y+root->val;
        ans = max(ans, helper(root->left)+helper(root->right));
        return(ans);
    }   

    int helper1(TreeNode* root){
        int cur = 0;
        queue<TreeNode*> q;
        q.push(root);
        vector<pair<TreeNode*, int>> pq;

        while(q.size() > 0){
            int l = q.size();
            while(l--){
                auto r = q.front();
                q.pop();
                pq.push_back({r,cur});
                if(r->left != nullptr)q.push(r->left);
                if(r->right != nullptr)q.push(r->right);
            }
            cur++;
        }
        
        int l = pq.size();
        int x,y;
        mp[0] = 0;
        int ans = 0;
        for(int i=l-1;i>=0;i--){
            auto r = pq[i].first;
            if(r->left == nullptr && r->right == nullptr){mp[r] = r->val;continue;}
            if(r->left != nullptr){x = mp[r->left->left] + mp[r->left->right];}
            else x = 0;
            if(r->right != nullptr){y = mp[r->right->left] + mp[r->right->right];}
            else y = 0;
            ans = x+y+r->val;
            ans = max(ans, mp[r->left]+mp[r->right]);
            mp[r] = ans;
        }
        return(mp[root]);
    }

    int rob(TreeNode* root) {
        return(helper1(root));
    }
};
21654