<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) {
        int ans_result = 0;

        node_stack.push_back(root);
        int scan_size = 1;

        int cur_level_num = 0;
        int start_index = 0;
        int end_index = 0;

        // Construct rec map firstly
        while(1){
            if( scan_size == 0 ){
                break;
            }

            cur_level_num = 0;
            start_index = node_stack.size() - scan_size;
            end_index = node_stack.size();

            for(int scan_index = start_index;scan_index < end_index;scan_index++){
                if(node_stack[scan_index]->left != nullptr){
                    node_stack.push_back(node_stack[scan_index]->left);
                    cur_level_num++;
                }

                if(node_stack[scan_index]->right != nullptr){
                    node_stack.push_back(node_stack[scan_index]->right);
                    cur_level_num++;
                }
            }

            scan_size = cur_level_num;

        }

      
        start_index = node_stack.size() - 1;
        end_index = 0;

        for(int scan_index = start_index;scan_index >= 0;scan_index--){
            
            get_max(node_stack[scan_index],false,ans_result);
        }
        
        //get_max(root,false,ans_result);

        return ans_result;
       
    }

    void get_max(TreeNode* root,bool is_skip_self,int &sub_max){
        int left_max_saved = -1;
        int right_max_saved = -1;

        if( root == nullptr ){
            sub_max = 0;
            return;
        }

        if( root->left != nullptr){
            left_max_saved = root->left->val;
        }

        if( root->right != nullptr){
            right_max_saved =  root->right->val;
        }

        if(is_skip_self){
            if( root->left == nullptr ){
                if( root->right == nullptr ){
                    sub_max = 0;
                    
                    return;
                }
            }
        }

        if(is_skip_self){
            if( root->right == nullptr ){
                if( left_max_saved != -1 ){
                    sub_max = left_max_saved;
                    return;
                }
#if 0
                int left_max = 0;
                get_max(root->left,false,left_max);
                sub_max = left_max;
#endif
                sub_max = root->right->val;
                
                return;
            }
        }

        if(is_skip_self){
            if( root->left == nullptr ){
                if( right_max_saved != -1 ){
                    sub_max = right_max_saved;
                    return;
                }
#if 0
                int right_max = 0;
                get_max(root->right,false,right_max);
                sub_max = right_max;

#endif  
                sub_max = root->left->val;
                return;

            }
        }

        if( root->left == nullptr ){
            if( root->right == nullptr ){
                sub_max = root->val;
                return;
            }
        }

       

        int root_not_selected = 0;

        if( root->left == nullptr ){
           root_not_selected = root->right->val;
          

        }else if( root->right == nullptr ){
            root_not_selected = root->left->val;
            
            
        }else{
            root_not_selected = root->left->val + root->right->val;
        }

        // Root not selected   
        if(is_skip_self){
            sub_max = root_not_selected;
            return;
        }

        // Root selected
        int root_selected = root->val;

        {
            int left_max = 0;
            if( root->left != nullptr ){
                get_max(root->left,true,left_max);
            }
            
            int right_max = 0;
            if( root->right != nullptr ){
                get_max(root->right,true,right_max);
            }
            
            root_selected = root_selected + left_max + right_max;
        }
        
        sub_max = max(root_selected,root_not_selected);

        root->val = sub_max;
        return;
    }


private:
   
    vector<TreeNode*> node_stack;
};
18485