<p>You are given the <code>root</code> of a binary tree. We install cameras on the tree nodes where each camera at a node can monitor its parent, itself, and its immediate children.</p>

<p>Return <em>the minimum number of cameras needed to monitor all nodes of the tree</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/29/bst_cameras_01.png" style="width: 138px; height: 163px;" />
<pre>
<strong>Input:</strong> root = [0,0,null,0,0]
<strong>Output:</strong> 1
<strong>Explanation:</strong> One camera is enough to monitor all nodes if placed as shown.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/29/bst_cameras_02.png" style="width: 139px; height: 312px;" />
<pre>
<strong>Input:</strong> root = [0,0,null,0,null,0,null,null,0]
<strong>Output:</strong> 2
<strong>Explanation:</strong> At least two cameras are needed to monitor all nodes of the tree. The above image shows one of the valid configurations of camera placement.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li>
	<li><code>Node.val == 0</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:
    map<TreeNode*, pair<int, int>> m;
    int minCameraCover(TreeNode* root) {
        buildmap(root);
        return min(m[root].first, m[root].second);
    }
    void buildmap(TreeNode* root){
        if(root -> right) buildmap(root -> right);
        if(root -> left) buildmap(root -> left);
        if(root -> right == nullptr && root -> left == nullptr) m[root] = {1, 1};
        else if(root -> right == nullptr){
            pair<int,int> p;
            p.first = 1;
            if(root -> left -> right) p.first += min((m[root -> left -> right]).first, (m[root -> left -> right]).second);
            if(root -> left -> left) p.first += min(m[root -> left -> left].first, m[root -> left -> left].second);
            p.second = 0;
            p.second += m[root -> left].first;
            m[root] = p;
        }
        else if(root -> left == nullptr){
            pair<int,int> p;
            p.first = 1;
            if(root -> right -> right) p.first += min(m[root -> right -> right].first, m[root -> right -> right].second);
            if(root -> right -> left) p.first += min(m[root -> right -> left].first, m[root -> right -> left].second);
            p.second = 0;
            p.second += m[root -> right].first;
            m[root] = p;
        }
        else{
            pair<int,int> p;
            p.first = 1;
            int reg = 0, lef = 0;
            if(root -> right -> right) reg += min(m[root -> right -> right].first, m[root -> right -> right].second);
            if(root -> right -> left) reg += min(m[root -> right -> left].first, m[root -> right -> left].second);
            if(root -> left -> right) lef += min(m[root -> left -> right].first, m[root -> left -> right].second);
            if(root -> left -> left) lef += min(m[root -> left -> left].first, m[root -> left -> left].second);
            reg = min(reg, min(m[root -> right].first, m[root -> right].second));
            lef = min(lef, min(m[root -> left].first, m[root -> left].second));
            p.first += reg;
            p.first += lef;
            p.second = 0;
            p.second = min(m[root -> left].first + min(m[root -> right].first, m[root -> right].second), m[root -> right].first + min(m[root -> left].first, m[root -> left].second));
            m[root] = p;
        }   
    }
};
24000