<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{
private:
    map<pair<TreeNode *, pair<bool, bool>>, int> dp;
public:
    int minCmera(TreeNode *root, bool parent_not_having_camera, bool necessary)
    {
        if (root == NULL) return 0;
        
        //Leaf node that needs to be covered
        //has no children to take care of it so install a camera
        if (!root->left && !root->right && parent_not_having_camera) return 1;

        if (dp.find({root, {parent_not_having_camera, necessary}}) != dp.end())
            return dp[{root, {parent_not_having_camera, necessary}}];

        //has to cover for parent
        if (necessary)
            return dp[{root, {parent_not_having_camera, necessary}}] =
                       1 + minCmera(root->left, false, false) + 
                       minCmera(root->right, false, false);

        //not being covered by its parent
        if (parent_not_having_camera){
            //install a camera
            int install = 1 + minCmera(root->left, false, false) +
                          minCmera(root->right, false, false);
            //let its one of the children take care of it
            int notInstall;
            //only 1 child, so that child has to cover for parent
            if (root->left == NULL)
                notInstall = minCmera(root->right, true, true);
            else if (root->right == NULL)
                notInstall = minCmera(root->left, true, true);
            //2 children, so we've a choice -- take the minimum
            else
                notInstall = min(minCmera(root->left, true, true) +
                                     minCmera(root->right, true, false),
                                 minCmera(root->left, true, false) +
                                     minCmera(root->right, true, true));

            return dp[{root, {parent_not_having_camera, necessary}}] =
                                                     min(install, notInstall);
        }

        //being covered by parent -> so we've choice
        //1. install 2. not install
        // choose the minimum
        return dp[{root, {parent_not_having_camera, necessary}}] =
                   min(1 + minCmera(root->left, false, false) +
                           minCmera(root->right, false, false),
                       minCmera(root->left, true, false) +
                       minCmera(root->right, true, false));
    }

public:
    int minCameraCover(TreeNode *root){
        dp.clear();
        return minCmera(root, true, false);
    }
};
28200