<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:
    unordered_map<TreeNode*, int> minCamInc, minCamExcCov, minCamExcNcov;
    const int INF = 100000000;
    int minCoverInc(TreeNode *root) {
        // cout << "Called inc for " << root << endl;
        if (minCamInc.count(root))
            return minCamInc[root];
        int ans = 0;
        if (root->left != NULL)
            ans += min(minCoverExcCov(root->left), minCoverInc(root->left));
        if (root->right != NULL)
            ans += min(minCoverExcCov(root->right), minCoverInc(root->right));
        if (ans >= INF)
            ans = INF-1;
        // cout << "Returned " << ans+1 << endl;
        return minCamInc[root] = min(ans+1, INF);
    }

    int minCoverExcNcov(TreeNode *root)
    {
        if (minCamExcNcov.count(root))
            return minCamExcNcov[root];
        int ans = INF;
        if (root->right != NULL && root->left != NULL)
        {
            ans = minCoverInc(root->right) + minCoverInc(root->left);
            ans = min(ans, minCoverInc(root->right)+minCoverExcNcov(root->left));
            ans = min(ans, minCoverExcNcov(root->right)+minCoverInc(root->left));
        }
        else if (root->left != NULL)
        {
            ans = minCoverInc(root->left);
        }
        else if (root->right != NULL)
        {
            ans = minCoverInc(root->right);
        }
        return minCamExcNcov[root] = min(INF, ans);
    }

    int minCoverExcCov(TreeNode *root) {
        // cout << "Called exc for " << root << " " << covered << endl;  
        if (minCamExcCov.count(root))
            return minCamExcCov[root];
        int ans = 0;
        if (root->left != NULL)
            ans += min(minCoverInc(root->left), minCoverExcNcov(root->left));
        if (root->right != NULL)
            ans += min(minCoverInc(root->right), minCoverExcNcov(root->right));
        return minCamExcCov[root] = min(INF, ans);
    }

    void display(TreeNode *root)
    {
        if (root == NULL)
            return;
        cout << minCamInc[root] << " " << minCamExcCov[root] << " " << minCamExcNcov[root] << endl;
        display(root->left);
        display(root->right);
    }

public:
    int minCameraCover(TreeNode* root) {
        if (root == NULL)
            return 0;
        // int inc = minCamInc.count(root), exc = minCamExc.count(root);
        // if (inc && exc)
        //     return min(minCamInc[root], minCamExc[root]);
        // else if (inc)
        //     return min
        int inc = minCoverInc(root), exc = minCoverExcNcov(root);
        display(root);
        // if (exc == 0)
        //     return inc;
        return min(inc, exc);
    }
};
28100