<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 getValue(TreeNode* node,vector<int>& vec){
        fill(vec.begin(), vec.end(), 0);
        vec[0] = node->val;
        if(node->left != NULL)
        {
            vec[1] = node->left->val;
            if(node->left->left != NULL)
                vec[3] = node->left->left->val;
            if(node->left->right != NULL)
                vec[4] = node->left->right->val;
        }
        if(node->right != NULL)
        {
            vec[2] = node->right->val;
            if(node->right->left != NULL)
                vec[5] = node->right->left->val;
            if(node->right->right != NULL)
                vec[6] = node->right->right->val;
        }
        return max(vec[0]+vec[3]+vec[4]+vec[5]+vec[6],vec[1]+vec[2]);
    }

    int rob(TreeNode* root) {
        TreeNode* node = root;
        stack<TreeNode*> sk;
        TreeNode* last = NULL;
        vector<TreeNode*> res;
        vector<int> vec(7,0);
        while(node || !sk.empty()){
            while(node)
            {
                sk.push(node);
                node = node->left;
            }
            node = sk.top();

            if(!node->right || node->right == last){
                res.push_back(node);
                sk.pop();
                last = node;
                node = NULL;
            }
            else{
                node = node->right;
            }
        }
        for(int i=0;i<res.size();i++)
            res[i]->val = getValue(res[i],vec);
        return root->val;
    }
};
16795