<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 ans[10010][2];
    void print(TreeNode *root){
        if(root->left) print(root->left);
        if(root->right) print(root->right);
        cout<<root->val<<' ';
    }
    void traverse(TreeNode *root,TreeNode *x,int &c){
        //cout<<c<<' ';
        //x=new TreeNode(c);
        if(root->left){
            x->left=new TreeNode(c++);
            traverse(root->left,x->left,c);
        } 
        if(root->right){
            x->right=new TreeNode(c++);
            traverse(root->right,x->right,c);
        }
    }
    int dp(TreeNode *root,TreeNode *x,int check){
        if(root==NULL || x==NULL) return 0;
        if(ans[x->val][check]!=-1) return ans[x->val][check];
        //cout<<root->val<<' ';
        int f=dp(root->left,x->left,0)+dp(root->right,x->right,0);
        if(check==0){
            f=max(root->val+dp(root->left,x->left,1)+dp(root->right,x->right,1),f);
        }
        return ans[x->val][check]=f;
    }
    int rob(TreeNode* root){
        memset(ans,-1,sizeof(ans));
        TreeNode *x=new TreeNode(0);
        // TreeNode *y=x,*z=x,*h=x;
        // TreeNode *d=root,*e=root,*a=root,*b=root;
        int c=1;
        // print(root);cout<<endl;
         traverse(root,x,c);
        // print(x);cout<<endl;
        //print(root);cout<<endl;
        cout<<endl;
        return max(dp(root,x,0),dp(root,x,1));
    }
};
17218