<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*, vector<int>> m1;

    int func(TreeNode* root, bool curCam, bool parCam)
    {
        if(root == NULL && curCam == true) return 1e5;
        if(root == NULL && curCam == false) return 0;

        if(m1.count(root) == 0) m1[root] = vector<int> (4,-1);
        else
        {
            if(curCam == false && parCam == false && m1[root][0] != -1)
            {
                return m1[root][0];
            }
            else if(curCam == false && parCam == true && m1[root][1] != -1)
            {
                return m1[root][1];
            }
            else if(curCam == true && m1[root][2] != -1)
            {
                return m1[root][2];
            }
        }

        int lff = func(root->left, false, false);
        int ltf = func(root->left, true, false);
        int lft = func(root->left, false, true);
        int ltt = func(root->left, true, true);

        int rff = func(root->right, false, false);
        int rtf = func(root->right, true, false);
        int rft = func(root->right, false, true);
        int rtt = func(root->right, true, true);

        if(curCam == false && parCam == false)
        {
            // need to have a cam at atleast one child
            return m1[root][0] = min({1 + ltf + rff, 1 + lff + rtf, 2 + ltf + rtf});
        }
        else if(curCam == false && parCam == true)
        {
            return m1[root][1] = min(lff, 1+ltf) + min(rff, 1+rtf);
        }

        return m1[root][2] = min(lft, 1+ltt) + min(rft, 1+rtt);
    }
    

    int minCameraCover(TreeNode* root) {
        m1.clear();
        int val1 = func(root, false, false);
        int val2 = 1 + func(root, true, false);

        return min(val1, val2);
    }
};
26400